Ruby on Rails Routing - Simple Examples

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值