Ruby on Rails
NamedRoutes (Version #264)

Named Routes allow you to reduce code duplication by associating a name with a given route rule which generates a convenience method that wraps the route rule hash..

You define a named route by calling it in your routes.rb in place of the
connect method. So, for example:


  map.home '', :controller => 'main', :action => 'start'

This does a few things for you. First it creates a method called home_url.

So with the above named route, what would have previously been


  redirect_to :controller => 'main', :action => 'start'

is now


  redirect_to home_url

Because of the way the *_to methods are written, in the above case, where you don’t have any parameters to pass the named route, you could also pass it a method reference.


  redirect_to :home_url

Note: Calling named routes as symbols is deprecated and will be removed entirely in Rails 2.0

This can be used in tests as well. If you redirect_to home_url in your controller, you can assert_redirected_to home_url as well.

Behind the scenes the *_url method wraps a call to url_for on the route hash.

The *_url method takes a single optional argument which is a hash that is merged on top of the url_for’ed route. This makes it so that a given named route can be parametrized when used with redirect_to, link_to, etc.

If you had a route like this:


  map.user_page 'users/:user', :controller => 'users', :action => 'show'

You could do

  link_to @user.username, user_page_url(:user => @user)

Note that just like normal routes, the form is path_part/:symbol. So in the above example, the users part will only show up in the urls and doesn’t strictly correlate to anything in the controller. Note that the users/ is the only part of the path generated. The above example generates <a href="http://localhost/users/7">http://localhost/users/7</a>, for instance. The :user is the name of the parameter you’re defining. I’m not sure what happens if you make multiple routes with exact same path; probably not what you’d expect. For routes that define default values for route parameters, those defaults are honored if nothing is passed in.

When testing named routes with parameters, you have to also pass the parameters to the named route in the assert. So if you have redirect_to user_page_url(:user => @user) in your controller, you need to have assert_redirected_to user_page_url(:user => @user). The id of the @user in the controller has to match the id @user in the test. When you test assert_redirected_to all the parameters have to match.

In addition to the [route_name]_url method, you get a
hash_for_[route_name]_url method which just wraps the hash without calling url_for on it.

The *_url and hash_for_*_url methods are made available to controllers and views.

Route recognition and all that is afforded you with map.connect remains the case with named routes.

See also Routes for more resources on map.connect and other routing usage.

Named Routes allow you to reduce code duplication by associating a name with a given route rule which generates a convenience method that wraps the route rule hash..

You define a named route by calling it in your routes.rb in place of the
connect method. So, for example:


  map.home '', :controller => 'main', :action => 'start'

This does a few things for you. First it creates a method called home_url.

So with the above named route, what would have previously been


  redirect_to :controller => 'main', :action => 'start'

is now


  redirect_to home_url

Because of the way the *_to methods are written, in the above case, where you don’t have any parameters to pass the named route, you could also pass it a method reference.


  redirect_to :home_url

Note: Calling named routes as symbols is deprecated and will be removed entirely in Rails 2.0

This can be used in tests as well. If you redirect_to home_url in your controller, you can assert_redirected_to home_url as well.

Behind the scenes the *_url method wraps a call to url_for on the route hash.

The *_url method takes a single optional argument which is a hash that is merged on top of the url_for’ed route. This makes it so that a given named route can be parametrized when used with redirect_to, link_to, etc.

If you had a route like this:


  map.user_page 'users/:user', :controller => 'users', :action => 'show'

You could do

  link_to @user.username, user_page_url(:user => @user)

Note that just like normal routes, the form is path_part/:symbol. So in the above example, the users part will only show up in the urls and doesn’t strictly correlate to anything in the controller. Note that the users/ is the only part of the path generated. The above example generates <a href="http://localhost/users/7">http://localhost/users/7</a>, for instance. The :user is the name of the parameter you’re defining. I’m not sure what happens if you make multiple routes with exact same path; probably not what you’d expect. For routes that define default values for route parameters, those defaults are honored if nothing is passed in.

When testing named routes with parameters, you have to also pass the parameters to the named route in the assert. So if you have redirect_to user_page_url(:user => @user) in your controller, you need to have assert_redirected_to user_page_url(:user => @user). The id of the @user in the controller has to match the id @user in the test. When you test assert_redirected_to all the parameters have to match.

In addition to the [route_name]_url method, you get a
hash_for_[route_name]_url method which just wraps the hash without calling url_for on it.

The *_url and hash_for_*_url methods are made available to controllers and views.

Route recognition and all that is afforded you with map.connect remains the case with named routes.

See also Routes for more resources on map.connect and other routing usage.