Ruby on Rails latest stable (v4.1.8) - 0 notes - Class: RoutingUrlFor
url_for(options = nil) public










Relying on named routes

Passing a record (like an Active Record) 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).

Implicit Controller Namespacing

Controllers passed in using the :controller option will retain their namespace unless it is an absolute one.


<%= url_for(action: 'index') %>
# => /blog/

<%= url_for(action: 'find', controller: 'books') %>
# => /books/find

<%= url_for(action: 'login', controller: 'members', only_path: false, protocol: 'https') %>

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

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

<%= url_for(Workshop.new) %>
# => /workshops

<%= url_for(@workshop) %>
# calls @workshop.to_param which by default returns the id
# => /workshops/5

# => /workshops/1-workshop-name

# => http://www.example.com

<%= url_for(:back) %>
# => http://www.example.com

<%= url_for(:back) %>
# => javascript:history.back()

<%= url_for(action: 'index', controller: 'users') %>
# Assuming an "admin" namespace
# => /admin/users

<%= url_for(action: 'index', controller: '/users') %>
# Specify absolute path with beginning slash
# => /users
Show source
Register or log in to add new notes.