url_for(options = {}) public

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 http://example.com/controller/action.

When called from a view, url_for returns an HTML escaped url. If you need an unescaped url, pass :escape => false in the 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) (true by default unless :host is 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 :password is also present).
  • :password - Inline HTTP authentication (only plucked out if :user is also present).
  • :escape - Determines whether the returned URL will be HTML escaped or not (true by default).

Relying on named routes

If you instead of a hash pass a record (like an Active Record or Active Resource) as the options parameter, you’ll 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).


  <%= 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.railsapplication.com/members/login/

  <%= url_for(:action => 'play', :anchor => 'player') %>
  # => /messages/play/#player

  <%= url_for(:action => 'checkout', :anchor => 'tax&ship') %>
  # => /testing/jump/#tax&amp;ship

  <%= url_for(:action => 'checkout', :anchor => 'tax&ship', :escape => false) %>
  # => /testing/jump/#tax&ship

  <%= url_for(Workshop.new) %>
  # relies on Workshop answering a new_record? call (and in this case returning true)
  # => /workshops

  <%= url_for(@workshop) %>
  # calls @workshop.to_s
  # => /workshops/5

  <%= 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()
Show source
Register or log in to add new notes.
March 20, 2009
9 thanks

Use the current URL, with changes

You can use the current URL, whatever it is, with changes, as in:

# Create a link to the current page in RSS form
url_for(:overwrite_params => {:format => :rss})

This can be super-helpful because it preserves any GET params (like search parameters)

March 3, 2011
8 thanks

Using namespaces

If you are using a namespace in your routes.rb, for example:

namespace :admin do
  resources :products

then you can:

url_for([:admin, @product])


url_for([:edit, :admin, @product])
August 24, 2012
0 thanks

Specify :host option in emails

Emails need a fully qualified URL (with domain). Use the :host parameter.

But note also that you need to specify a value that is not dependent upon the request context. http://api.rubyonrails.org/classes/ActionMailer/Base.html recommends setting a default host in application.rb For those of us who have development, test, staging and production environments, set in the environment-specific files, or in the :default hash in the mailer.

This applies to both +url_for(:host => “example.com”)+ and when using named routes as in +widgets_url(:host => “example.com”)+