Changeset 6674
- Timestamp:
- 05/06/07 04:29:42 (2 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_view/helpers/url_helper.rb (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r6673 r6674 1 1 *SVN* 2 3 * Enhance documentation and add examples for url_for. [jeremymcanally] 2 4 3 5 * Fix documentation typo in routes. [norbert, pam] trunk/actionpack/lib/action_view/helpers/url_helper.rb
r6405 r6674 3 3 module ActionView 4 4 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. 8 9 module UrlHelper 9 10 include JavaScriptHelper 10 11 11 12 # 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 the13 # documentation for ActionController::Base#url_for . Note that it'll14 # set :only_path => trueso you'll get the relative /controller/action15 # 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. 16 17 # 17 18 # When called from a view, url_for returns an HTML escaped url. If you 18 19 # 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&ship 47 # 48 # <%= url_for(:action => 'checkout', :anchor => 'tax&ship', :escape => false) %> 49 # # => /testing/jump/#tax&ship 19 50 def url_for(options = {}, *parameters_for_method_reference) 20 51 if options.kind_of? Hash … … 31 62 # Creates a link tag of the given +name+ using a URL created by the set 32 63 # of +options+. See the valid options in the documentation for 33 # ActionController::Base#url_for. It's also possible to pass a string instead64 # url_for. It's also possible to pass a string instead 34 65 # of an options hash to get a link tag that uses the value of the string as the 35 66 # href for the link. If nil is passed as a name, the link itself will become 36 67 # the name. 37 68 # 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 42 71 # prompt with the question specified. If the user accepts, the link is 43 72 # processed normally, otherwise no action is taken. 44 # * <tt>:popup => true || array of window options</tt> :This will force the73 # * <tt>:popup => true || array of window options</tt> -- This will force the 45 74 # link to open in a popup window. By passing true, a default browser window 46 75 # will be opened with the URL. You can also specify an array of options 47 76 # that are passed-thru to JavaScripts window.open method. 48 # * <tt>:method => symbol of HTTP verb</tt> :This modifier will dynamically77 # * <tt>:method => symbol of HTTP verb</tt> -- This modifier will dynamically 49 78 # create an HTML form and immediately submit the form for processing using 50 79 # the HTTP verb specified. Useful for having links perform a POST operation … … 55 84 # for it in your controllers action by using the request objects methods 56 85 # for post?, delete? or put?. 86 # * The +html_options+ will accept a hash of html attributes for the link tag. 57 87 # 58 88 # You can mix and match the +html_options+ with the exception of … … 60 90 # exception. 61 91 # 92 # ==== Examples 62 93 # 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 # 63 96 # link_to "Help", { :action => "help" }, :popup => true 97 # # => <a href="/testing/help/" onclick="window.open(this.href);return false;">Help</a> 98 # 64 99 # 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 # 65 102 # 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> 66 107 def link_to(name, options = {}, html_options = nil, *parameters_for_method_reference) 67 108 if html_options … … 91 132 # is given, it will default to performing a POST operation. You can also 92 133 # 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 #102 134 # If you are using RESTful routes, you can pass the <tt>:method</tt> 103 135 # to change the HTTP verb used to submit the form. 104 136 # 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 # 105 153 # button_to "Delete Image", { :action => "delete", :id => @image.id }, 106 154 # :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>" 117 162 def button_to(name, options = {}, html_options = {}) 118 163 html_options = html_options.stringify_keys … … 141 186 142 187 # Creates a link tag of the given +name+ using a URL created by the set of 143 # +options+ unless the current request uriis the same as the links, in188 # +options+ unless the current request URI is the same as the links, in 144 189 # 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... 146 196 # 147 197 # <ul id="navbar"> … … 150 200 # </ul> 151 201 # 152 # This will render the following HTML when on the about us page:202 # If in the "about" action, it will render... 153 203 # 154 204 # <ul id="navbar"> … … 156 206 # <li>About Us</li> 157 207 # </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 # %> 158 225 def link_to_unless_current(name, options = {}, html_options = {}, *parameters_for_method_reference, &block) 159 226 link_to_unless current_page?(options), name, options, html_options, *parameters_for_method_reference, &block … … 162 229 # Creates a link tag of the given +name+ using a URL created by the set of 163 230 # +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 167 236 # <%= 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> 174 249 def link_to_unless(condition, name, options = {}, html_options = {}, *parameters_for_method_reference, &block) 175 250 if condition … … 189 264 # accepts the name or the full argument list for link_to_unless (see the examples 190 265 # 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> 191 281 def link_to_if(condition, name, options = {}, html_options = {}, *parameters_for_method_reference, &block) 192 282 link_to_unless !condition, name, options, html_options, *parameters_for_method_reference, &block … … 195 285 # Creates a mailto link tag to the specified +email_address+, which is 196 286 # also used as the name of the link unless +name+ is specified. Additional 197 # htmlattributes for the link can be passed in +html_options+.287 # HTML attributes for the link can be passed in +html_options+. 198 288 # 199 289 # mail_to has several methods for hindering email harvestors and customizing 200 290 # the email itself by passing special keys to +html_options+. 201 291 # 202 # Special HTML Options: 203 # 292 # ==== Options 204 293 # * <tt>:encode</tt> - This key will accept the strings "javascript" or "hex". 205 294 # Passing "javascript" will dynamically create and encode the mailto: link then … … 220 309 # * <tt>:bcc</tt> - Blind Carbon Copy additional recipients on the email. 221 310 # 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> 232 323 # 233 324 # 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> 236 327 def mail_to(email_address, name = nil, html_options = {}) 237 328 html_options = html_options.stringify_keys … … 281 372 end 282 373 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 284 390 def current_page?(options) 285 391 url_string = CGI.escapeHTML(url_for(options))