Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6674

Show
Ignore:
Timestamp:
05/06/07 04:29:42 (2 years ago)
Author:
marcel
Message:

Enhance documentation and add examples for url_for. Closes #8227. [jeremymcanally]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r6673 r6674  
    11*SVN* 
     2 
     3* Enhance documentation and add examples for url_for. [jeremymcanally] 
    24 
    35* Fix documentation typo in routes. [norbert, pam] 
  • trunk/actionpack/lib/action_view/helpers/url_helper.rb

    r6405 r6674  
    33module ActionView 
    44  module Helpers #:nodoc: 
    5     # Provides a set of methods for making easy links and getting urls that 
    6     # depend on the controller and action. This means that you can use the 
    7     # same format for links in the views that you do in the controller. 
     5    # Provides a set of methods for making links and getting URLs that 
     6    # depend on the routing subsystem (see ActionController::Routing).  
     7    # This allows you to use the same format for links in views  
     8    # and controllers. 
    89    module UrlHelper 
    910      include JavaScriptHelper 
    1011 
    1112      # Returns the URL for the set of +options+ provided. This takes the 
    12       # same options as url_for in action controller. For a list, see the 
    13       # documentation for ActionController::Base#url_for. Note that it'll 
    14       # set :only_path => true so you'll get the relative /controller/action 
    15       # instead of the fully qualified http://example.com/controller/action. 
     13      # same options as url_for in ActionController (see the 
     14      # documentation for ActionController::Base#url_for). Note that by default 
     15      # <tt>:only_path</tt> is <tt>true</tt> so you'll get the relative /controller/action 
     16      # instead of the fully qualified URL like http://example.com/controller/action. 
    1617      # 
    1718      # When called from a view, url_for returns an HTML escaped url. If you 
    1819      # need an unescaped url, pass :escape => false in the +options+. 
     20      # 
     21      # ==== Options 
     22      # * <tt>:anchor</tt> -- specifies the anchor name to be appended to the path. 
     23      # * <tt>:only_path</tt> --  if true, returns the relative URL (omitting the protocol, host name, and port) (<tt>true</tt> by default) 
     24      # * <tt>:trailing_slash</tt> --  if true, adds a trailing slash, as in "/archive/2005/". Note that this 
     25      #   is currently not recommended since it breaks caching. 
     26      # * <tt>:host</tt> -- overrides the default (current) host if provided 
     27      # * <tt>:protocol</tt> -- overrides the default (current) protocol if provided 
     28      # * <tt>:user</tt> -- Inline HTTP authentication (only plucked out if :password is also present) 
     29      # * <tt>:password</tt> -- Inline HTTP authentication (only plucked out if :user is also present) 
     30      # * <tt>:escape</tt> -- Determines whether the returned URL will be HTML escaped or not (<tt>true</tt> by default) 
     31      # 
     32      # ==== Examples 
     33      #   <%= url_for(:action => 'index') %> 
     34      #   # => /blog/ 
     35      # 
     36      #   <%= url_for(:action => 'find', :controller => 'books') %> 
     37      #   # => /books/find 
     38      # 
     39      #   <%= url_for(:action => 'login', :controller => 'members', :only_path => false, :protocol => 'https') %> 
     40      #   # => https://www.railsapplication.com/members/login/ 
     41      # 
     42      #   <%= url_for(:action => 'play', :anchor => 'player') %> 
     43      #   # => /messages/play/#player 
     44      # 
     45      #   <%= url_for(:action => 'checkout', :anchor => 'tax&ship') %> 
     46      #   # => /testing/jump/#tax&amp;ship 
     47      # 
     48      #   <%= url_for(:action => 'checkout', :anchor => 'tax&ship', :escape => false) %> 
     49      #   # => /testing/jump/#tax&ship 
    1950      def url_for(options = {}, *parameters_for_method_reference) 
    2051        if options.kind_of? Hash 
     
    3162      # Creates a link tag of the given +name+ using a URL created by the set 
    3263      # of +options+. See the valid options in the documentation for 
    33       # ActionController::Base#url_for. It's also possible to pass a string instead 
     64      # url_for. It's also possible to pass a string instead 
    3465      # of an options hash to get a link tag that uses the value of the string as the 
    3566      # href for the link. If nil is passed as a name, the link itself will become 
    3667      # the name. 
    3768      # 
    38       # The +html_options+ will accept a hash of html attributes for the link tag. 
    39       # It also accepts 3 modifiers that specialize the link behavior. 
    40       # 
    41       # * <tt>:confirm => 'question?'</tt>: This will add a JavaScript confirm 
     69      # ==== Options 
     70      # * <tt>:confirm => 'question?'</tt> -- This will add a JavaScript confirm 
    4271      #   prompt with the question specified. If the user accepts, the link is 
    4372      #   processed normally, otherwise no action is taken. 
    44       # * <tt>:popup => true || array of window options</tt>: This will force the 
     73      # * <tt>:popup => true || array of window options</tt> -- This will force the 
    4574      #   link to open in a popup window. By passing true, a default browser window 
    4675      #   will be opened with the URL. You can also specify an array of options 
    4776      #   that are passed-thru to JavaScripts window.open method. 
    48       # * <tt>:method => symbol of HTTP verb</tt>: This modifier will dynamically 
     77      # * <tt>:method => symbol of HTTP verb</tt> -- This modifier will dynamically 
    4978      #   create an HTML form and immediately submit the form for processing using 
    5079      #   the HTTP verb specified. Useful for having links perform a POST operation 
     
    5584      #   for it in your controllers action by using the request objects methods 
    5685      #   for post?, delete? or put?. 
     86      # * The +html_options+ will accept a hash of html attributes for the link tag. 
    5787      # 
    5888      # You can mix and match the +html_options+ with the exception of 
     
    6090      # exception. 
    6191      # 
     92      # ==== Examples 
    6293      #   link_to "Visit Other Site", "http://www.rubyonrails.org/", :confirm => "Are you sure?" 
     94      #   # => <a href="http://www.rubyonrails.org/" onclick="return confirm('Are you sure?');">Visit Other Site</a> 
     95      # 
    6396      #   link_to "Help", { :action => "help" }, :popup => true 
     97      #   # => <a href="/testing/help/" onclick="window.open(this.href);return false;">Help</a> 
     98      # 
    6499      #   link_to "View Image", { :action => "view" }, :popup => ['new_window_name', 'height=300,width=600'] 
     100      #   # => <a href="/testing/view/" onclick="window.open(this.href,'new_window_name','height=300,width=600');return false;">View Image</a> 
     101      # 
    65102      #   link_to "Delete Image", { :action => "delete", :id => @image.id }, :confirm => "Are you sure?", :method => :delete 
     103      #   # => <a href="/testing/delete/9/" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form');  
     104      #        f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href; 
     105      #        var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');  
     106      #        m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete Image</a> 
    66107      def link_to(name, options = {}, html_options = nil, *parameters_for_method_reference) 
    67108        if html_options 
     
    91132      # is given, it will default to performing a POST operation. You can also 
    92133      # disable the button by passing <tt>:disabled => true</tt> in +html_options+. 
    93       # 
    94       #   button_to "New", :action => "new" 
    95       # 
    96       # Generates the following HTML: 
    97       # 
    98       #   <form method="post" action="/controller/new" class="button-to"> 
    99       #     <div><input value="New" type="submit" /></div> 
    100       #   </form> 
    101       # 
    102134      # If you are using RESTful routes, you can pass the <tt>:method</tt> 
    103135      # to change the HTTP verb used to submit the form. 
    104136      # 
     137      # ==== Options 
     138      # The +options+ hash accepts the same options at url_for. 
     139      # 
     140      # There are a few special +html_options+: 
     141      # * <tt>:method</tt> -- specifies the anchor name to be appended to the path. 
     142      # * <tt>:disabled</tt> -- specifies the anchor name to be appended to the path. 
     143      # * <tt>:confirm</tt> -- This will add a JavaScript confirm 
     144      #   prompt with the question specified. If the user accepts, the link is 
     145      #   processed normally, otherwise no action is taken. 
     146      #  
     147      # ==== Examples 
     148      #   <%= button_to "New", :action => "new" %> 
     149      #   # => "<form method="post" action="/controller/new" class="button-to"> 
     150      #   #      <div><input value="New" type="submit" /></div> 
     151      #   #    </form>" 
     152      # 
    105153      #   button_to "Delete Image", { :action => "delete", :id => @image.id }, 
    106154      #             :confirm => "Are you sure?", :method => :delete 
    107       # 
    108       # Which generates the following HTML: 
    109       # 
    110       #   <form method="post" action="/images/delete/1" class="button-to"> 
    111       #     <div> 
    112       #       <input type="hidden" name="_method" value="delete" /> 
    113       #       <input onclick="return confirm('Are you sure?');" 
    114       #                 value="Delete" type="submit" /> 
    115       #     </div> 
    116       #   </form> 
     155      #   # => "<form method="post" action="/images/delete/1" class="button-to"> 
     156      #   #      <div> 
     157      #   #        <input type="hidden" name="_method" value="delete" /> 
     158      #   #        <input onclick="return confirm('Are you sure?');" 
     159      #   #              value="Delete" type="submit" /> 
     160      #   #      </div> 
     161      #   #    </form>" 
    117162      def button_to(name, options = {}, html_options = {}) 
    118163        html_options = html_options.stringify_keys 
     
    141186 
    142187      # Creates a link tag of the given +name+ using a URL created by the set of 
    143       # +options+ unless the current request uri is the same as the links, in 
     188      # +options+ unless the current request URI is the same as the links, in 
    144189      # which case only the name is returned (or the given block is yielded, if 
    145       # one exists). Refer to the documentation for link_to_unless for block usage. 
     190      # one exists).  You can give link_to_unless_current a block which will  
     191      # specialize the default behavior (e.g., show a "Start Here" link rather 
     192      # than the link's text). 
     193      # 
     194      # ==== Examples 
     195      # Let's say you have a navigation menu... 
    146196      # 
    147197      #   <ul id="navbar"> 
     
    150200      #   </ul> 
    151201      # 
    152       # This will render the following HTML when on the about us page: 
     202      # If in the "about" action, it will render... 
    153203      # 
    154204      #   <ul id="navbar"> 
     
    156206      #     <li>About Us</li> 
    157207      #   </ul> 
     208      # 
     209      # ...but if in the "home" action, it will render: 
     210      # 
     211      #   <ul id="navbar"> 
     212      #     <li><a href="/controller/index">Home</a></li> 
     213      #     <li><a href="/controller/about">About Us</a></li> 
     214      #   </ul> 
     215      # 
     216      # The implicit block given to link_to_unless_current is evaluated if the current 
     217      # action is the action given.  So, if we had a comments page and wanted to render a  
     218      # "Go Back" link instead of a link to the comments page, we could do something like this... 
     219      #    
     220      #    <%=  
     221      #        link_to_unless_current("Comment", { :controller => 'comments', :action => 'new}) do 
     222      #           link_to("Go back", { :controller => 'posts', :action => 'index' })  
     223      #        end  
     224      #     %> 
    158225      def link_to_unless_current(name, options = {}, html_options = {}, *parameters_for_method_reference, &block) 
    159226        link_to_unless current_page?(options), name, options, html_options, *parameters_for_method_reference, &block 
     
    162229      # Creates a link tag of the given +name+ using a URL created by the set of 
    163230      # +options+ unless +condition+ is true, in which case only the name is 
    164       # returned. To specialize the default behavior, you can pass a block that 
    165       # accepts the name or the full argument list for link_to_unless (see the example). 
    166       # 
     231      # returned. To specialize the default behavior (i.e., show a login link rather 
     232      # than just the plaintext link text), you can pass a block that 
     233      # accepts the name or the full argument list for link_to_unless. 
     234      # 
     235      # ==== Examples 
    167236      #   <%= link_to_unless(@current_user.nil?, "Reply", { :action => "reply" }) %> 
    168       # 
    169       # This example uses a block to modify the link if the condition isn't met. 
    170       # 
    171       #   <%= link_to_unless(@current_user.nil?, "Reply", { :action => "reply" }) do |name| 
    172       #      link_to(name, { :controller => "accounts", :action => "signup" }) 
    173       #    end %> 
     237      #   # If the user is logged in... 
     238      #   # => <a href="/controller/reply/">Reply</a> 
     239      # 
     240      #   <%=  
     241      #      link_to_unless(@current_user.nil?, "Reply", { :action => "reply" }) do |name| 
     242      #        link_to(name, { :controller => "accounts", :action => "signup" }) 
     243      #      end  
     244      #   %> 
     245      #   # If the user is logged in... 
     246      #   # => <a href="/controller/reply/">Reply</a> 
     247      #   # If not... 
     248      #   # => <a href="/accounts/signup">Reply</a> 
    174249      def link_to_unless(condition, name, options = {}, html_options = {}, *parameters_for_method_reference, &block) 
    175250        if condition 
     
    189264      # accepts the name or the full argument list for link_to_unless (see the examples 
    190265      # in link_to_unless). 
     266      # 
     267      # ==== Examples 
     268      #   <%= link_to_if(@current_user.nil?, "Login", { :controller => "sessions", :action => "new" }) %> 
     269      #   # If the user isn't logged in... 
     270      #   # => <a href="/sessions/new/">Login</a> 
     271      # 
     272      #   <%=  
     273      #      link_to_if(@current_user.nil?, "Login", { :controller => "sessions", :action => "new" }) do 
     274      #        link_to(@current_user.login, { :controller => "accounts", :action => "show", :id => @current_user }) 
     275      #      end  
     276      #   %> 
     277      #   # If the user isn't logged in... 
     278      #   # => <a href="/sessions/new/">Login</a> 
     279      #   # If they are logged in... 
     280      #   # => <a href="/accounts/show/3">my_username</a> 
    191281      def link_to_if(condition, name, options = {}, html_options = {}, *parameters_for_method_reference, &block) 
    192282        link_to_unless !condition, name, options, html_options, *parameters_for_method_reference, &block 
     
    195285      # Creates a mailto link tag to the specified +email_address+, which is 
    196286      # also used as the name of the link unless +name+ is specified. Additional 
    197       # html attributes for the link can be passed in +html_options+. 
     287      # HTML attributes for the link can be passed in +html_options+. 
    198288      # 
    199289      # mail_to has several methods for hindering email harvestors and customizing 
    200290      # the email itself by passing special keys to +html_options+. 
    201291      # 
    202       # Special HTML Options: 
    203       # 
     292      # ==== Options 
    204293      # * <tt>:encode</tt>  - This key will accept the strings "javascript" or "hex". 
    205294      #   Passing "javascript" will dynamically create and encode the mailto: link then 
     
    220309      # * <tt>:bcc</tt>  - Blind Carbon Copy additional recipients on the email. 
    221310      # 
    222       # Examples: 
    223       #   mail_to "me@domain.com"  # => <a href="mailto:me@domain.com">me@domain.com</a> 
    224       #   mail_to "me@domain.com", "My email", :encode => "javascript"  # => 
    225       #     <script type="text/javascript">eval(unescape('%64%6f%63...%6d%65%6e'))</script> 
    226       # 
    227       #   mail_to "me@domain.com", "My email", :encode => "hex"  # => 
    228       #     <a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">My email</a> 
    229       # 
    230       #   mail_to "me@domain.com", nil, :replace_at => "_at_", :replace_dot => "_dot_", :class => "email"  # => 
    231       #     <a href="mailto:me@domain.com" class="email">me_at_domain_dot_com</a> 
     311      # ==== Examples 
     312      #   mail_to "me@domain.com"  
     313      #   # => <a href="mailto:me@domain.com">me@domain.com</a> 
     314      # 
     315      #   mail_to "me@domain.com", "My email", :encode => "javascript"   
     316      #   # => <script type="text/javascript">eval(unescape('%64%6f%63...%6d%65%6e'))</script> 
     317      # 
     318      #   mail_to "me@domain.com", "My email", :encode => "hex"   
     319      #   # => <a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">My email</a> 
     320      # 
     321      #   mail_to "me@domain.com", nil, :replace_at => "_at_", :replace_dot => "_dot_", :class => "email"   
     322      #   # => <a href="mailto:me@domain.com" class="email">me_at_domain_dot_com</a> 
    232323      # 
    233324      #   mail_to "me@domain.com", "My email", :cc => "ccaddress@domain.com", 
    234       #            :subject => "This is an example email"  # => 
    235       #    <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a> 
     325      #            :subject => "This is an example email"   
     326      #   # => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a> 
    236327      def mail_to(email_address, name = nil, html_options = {}) 
    237328        html_options = html_options.stringify_keys 
     
    281372      end 
    282373 
    283       # True if the current request uri was generated by the given +options+. 
     374      # True if the current request URI was generated by the given +options+. 
     375      # 
     376      # ==== Examples 
     377      # Let's say we're in the <tt>/shop/checkout</tt> action. 
     378      # 
     379      #   current_page?(:action => 'process') 
     380      #   # => false 
     381      # 
     382      #   current_page?(:controller => 'shop', :action => 'checkout') 
     383      #   # => true 
     384      # 
     385      #   current_page?(:action => 'checkout') 
     386      #   # => true 
     387      # 
     388      #   current_page?(:controller => 'library', :action => 'checkout') 
     389      #   # => false 
    284390      def current_page?(options) 
    285391        url_string = CGI.escapeHTML(url_for(options))