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
分享到:
相关推荐
Ruby on Rails Tutorial - Learn Web Development with Rails 3rd
原文是Web版本,已经导出成PDF版本供大家查看。原版是英文版的《Ruby on Rails Tutorial》,特别适合有其他语言开发经验的Rails入门。
Ruby on Rails Guides v2 - Ruby on Rails 4.2.5
Ruby on Rails入门经典-例子,有很多rails工程实例。
《Ruby on Rails Tutorial》中文版(原书第2版,涵盖 Rails 4) Ruby 是一门很美的计算机语言,其设计原则就是“让编程人员快乐”。David Heinemeier Hansson 就是看重了这一点,才在开发 Rails 框架时选择了 Ruby...
Ubuntu系统ruby on rails安装 Ubuntu系统ruby on rails安装 Ubuntu系统ruby on rails安装 Ubuntu系统ruby on rails安装 Ubuntu系统ruby on rails安装 Ubuntu系统ruby on rails安装 Ubuntu系统ruby on rails安装 ...
Ruby On Rails中文教材(PDF)
ruby on rails社区网站开发源码
Ruby on Rails 初体验--北大青鸟教师专题讲座PPT 想学Ruby的赶快下载看看。 Ruby--目前最快速开发工具
rails-dev-box, 面向 Ruby on Rails 核心开发的虚拟机 用于 Ruby on Rails 核心开发的虚拟机简介注意:这个虚拟机不是为 Rails 应用程序开发而设计的,只是为。 这个项目自动设置开发环境,以便在 Ruby on Rails ...
Ruby on Rails Web开发学习实录 内容简介: 在目前的主流web开发技术中,基于ruby语言的rails框架是做网站开发速度最快的工具。它可以达到j2ee框架开发速度的5~10倍,并且代码量也非常少。另外由于代码量的大幅度...
ruby on rails对mongodb的操作ruby on rails对mongodb的操作ruby on rails对mongodb的操作ruby on rails对mongodb的操作
Ruby on Rails与MongoDB 您可以在MongoDB的帮助下轻松... rails new ruby-on-rails-with-mongodb --skip-active-record从您的Gemfile中删除sqlite3(如果存在),将Mongoid添加到您的Gemfile中,然后运行“ bundle”。
本书的目标是成为对“如果想学习使用 Ruby on Rails 进行 Web 开发,我应该从哪儿开始?”这一问题的最好答案。学习完本书的内容之后,你将具备使用 Rails 进行开发和部署 Web 程序的技能。同时你还能够通过一些进阶...
本书教您如何使用Ruby on Rails开发和部署真正的,具有工业实力的Web应用程序,Ruby on Rails是为诸如Twitter,Hulu,GitHub和Yellow Pages等顶级网站提供支持的开源Web框架。
Ruby On Rails 官方教程,这本书讲解如何使用 Ruby on Rails 框架开发应用,以及如何把应用部署到生成环境。本书使用 Rails 默认的开发工具栈开发了一个完整的社交应用(类似 Twitter)。读完本书后你将掌握如何使用...
Ruby On Rails 框架自它提出之日起就受到广泛关注,在“不要重复自己”,“约定优于配置”等思想的指导下,Rails 带给 Web 开发者的是极高的开发效率。 ActiveRecord 的灵活让你再也不用配置繁琐的 Hibernate 即可...
ruby on rails 教程源码,配合原书使用