This article contains a list of ruby on rails routing examples. If you find you have any questions please leave a comment.
Routes are processed from the top of routes.rb down. If a route is matched it will stop processing the routes.rb file and use that route.
I’m detailing basic and named routes below. In most cases, you should elect to use named routes. However it is important that you understand basic routes.
Resources are a third type of route that are not covered here. I will be covering them later so please check back in a couple of weeks (I’ll remove this text and link to the new post when it’s available).
To access parameters that are passed in a url in the controller, use the params hash (array). E.g. to access the :year value specified in this route:
map.connect 'articles/:year', :controller => 'articles'
do the following in the controller:
def index
logger.debug params[:year]
end
Basic Routes ( map.connect )
map.connect ‘articles’, :controller => ‘articles’
The most basic route.
Example URLs:
/articles
Controller:
articles
Action:
index
Attributes available in [:params]:
none
Notes:
map.connect will default to index if there is no action specified.
map.connect ‘articles/:year’, :controller => ‘articles’, :action => ‘show’
A basic route with a placeholder (:year).
Acceptable URLs:
/articles/2007
Controller:
articles
Action:
show
Attributes available in [:params]:
:year
Notes:
:year will now be available in the show method within the articles controller. You can access it using params[:year].
map.connect ‘articles/:year/:month/:day’, :controller => ‘articles’, :action => ‘show’
This example shows the use of multiple placeholders in the URL.
Acceptable URLs:
/articles/2007/1/1
Controller:
articles
Action:
show
Attributes available in [:params]:
:year
:month
:day
map.connect ‘articles/:year/:month/:day’, :controller => ‘articles’, :action => ‘show’, :month => nil, :day => nil
We’re setting defaults for the :month and :day placeholders here. If they’re not passed in the URL, they will default to these values.
Example URLs:
/articles/2007/1/1
/articles/2007/1
/articles/2007
Controller:
articles
Action:
show
Attributes available in [:params]:
:year
:month
:day
map.connect ‘articles/:year/:month/:day’, :controller => ‘articles’, :action => ‘show’, :month => nil, :day => nil, :requirements => { :year => /\d{4}/ }
The :requirements option restricts the format of the placeholder values using a regular expression. In this example we’re saying the year can only be a 4 digit number.
Example URLs:
/articles/2007/1/1
/articles/2007
Controller:
articles
Action:
show
Attributes available in [:params]:
:year
:month
:day
[size=x-large][b]Named Routes ( map.whatever )[/b][/size]
map.home ‘/articles’, :controller => ‘articles’, :action => ‘show’
Example URLs:
/articles
Controller:
articles
Action:
show
Attributes available in [:params]:
none
Methods made available to you in the controller:
home_path (generate relative url e.g. /articles)
home_url (generate absolute url e.g. http://mysite.com/articles)
map.article_archive ‘articles/:year/:month’, :controller => ‘articles’, :action => ‘show’
Example URLs:
/articles/2007/1
Controller:
articles
Action:
show
Attributes available in [:params]:
:year
:month
Methods made available to you in the controller:
articlearchivepath() (generate relative url e.g. /articles/2007/1)
articlearchiveurl() (generate absolute url e.g. http://mysite.com/articles/2007/1)
Notes: In this example you need to pass the :year and :month when using the methods above. There are two ways of doing this:
articlearchivepath(2007, 5) outputs /articles/2007/5
articlearchivepath(:year => 2007, :month => 5) outputs /articles/2007/5