instance method
url_for
Ruby on Rails 3.1.12
Since v2.2.3 Last seen in v3.2.22.5Signature
url_for(options = {})
Returns the URL for the set of options provided. This takes the same options as url_for in Action Controller (see the documentation for ActionController::Base#url_for). Note that by default :only_path is true so you’ll get the relative “/controller/action” instead of the fully qualified URL like “example.com/controller/action”.
Options
-
:anchor- Specifies the anchor name to be appended to the path. -
:only_path- If true, returns the relative URL (omitting the protocol, host name, and port) (trueby default unless:hostis specified). -
:trailing_slash- If true, adds a trailing slash, as in “/archive/2005/”. Note that this is currently not recommended since it breaks caching. -
:host- Overrides the default (current) host if provided. -
:protocol- Overrides the default (current) protocol if provided. -
:user- Inline HTTP authentication (only plucked out if:passwordis also present). -
:password- Inline HTTP authentication (only plucked out if:useris also present).
Relying on named routes
Passing a record (like an Active Record or Active Resource) instead of a Hash as the options parameter will trigger the named route for that record. The lookup will happen on the name of the class. So passing a Workshop object will attempt to use the workshop_path route. If you have a nested route, such as admin_workshop_path you’ll have to call that explicitly (it’s impossible for url_for to guess that route).
Examples
<%= url_for(:action => 'index') %>
# => /blog/
<%= url_for(:action => 'find', :controller => 'books') %>
# => /books/find
<%= url_for(:action => 'login', :controller => 'members', :only_path => false, :protocol => 'https') %>
# => https://www.example.com/members/login/
<%= url_for(:action => 'play', :anchor => 'player') %>
# => /messages/play/#player
<%= url_for(:action => 'jump', :anchor => 'tax&ship') %>
# => /testing/jump/#tax&ship
<%= url_for(Workshop.new) %>
# relies on Workshop answering a persisted? call (and in this case returning false)
# => /workshops
<%= url_for(@workshop) %>
# calls @workshop.to_param which by default returns the id
# => /workshops/5
# to_param can be re-defined in a model to provide different URL names:
# => /workshops/1-workshop-name
<%= url_for("http://www.example.com") %>
# => http://www.example.com
<%= url_for(:back) %>
# if request.env["HTTP_REFERER"] is set to "http://www.example.com"
# => http://www.example.com
<%= url_for(:back) %>
# if request.env["HTTP_REFERER"] is not set or is blank
# => javascript:history.back()
Parameters
-
optionsopt = {}
Source
# File actionpack/lib/action_view/helpers/url_helper.rb, line 100
def url_for(options = {})
options ||= {}
case options
when String
options
when Hash
options = options.symbolize_keys.reverse_merge!(:only_path => options[:host].nil?)
super
when :back
controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
else
polymorphic_path(options)
end
end
Defined in actionpack/lib/action_view/helpers/url_helper.rb line 100
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionView::Helpers::UrlHelper