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,简称ROR或Rails,是一款基于Ruby语言的开源Web应用框架,它遵循Model-View-Controller(MVC)架构模式,旨在提高开发效率和代码可读性。本教程“Ruby on Rails 教程 - 201406”可能是针对2014年6月时...
### Ruby on Rails 教程 —— 使用 Rails 学习 Web 开发(第三版) #### 关于本书 本书《Ruby on Rails 教程 —— 使用 Rails 学习 Web 开发》第三版是由 Michael Hartl 编写的一本全面介绍 Ruby on Rails 的教程...
### Ruby on Rails Guides v2 - Ruby on Rails 4.2.5 #### 一、重要概念及基础假设 - **重要概念**:本指南旨在帮助读者深入理解Ruby on Rails(以下简称Rails)4.2.5版本的核心功能与最佳实践。 - **基础假设**:...
《Ruby on Rails Tutorial》中文版(原书第2版,涵盖 Rails 4) Ruby 是一门很美的计算机语言,其设计原则就是“让编程人员快乐”。David Heinemeier Hansson 就是看重了这一点,才在开发 Rails 框架时选择了 Ruby...
### Ruby on Rails 101:深入理解与实践 #### 引言 《Ruby on Rails 101》是一本介绍Ruby on Rails(简称RoR或ROR)的基础书籍,旨在为初学者提供一个全面而深入的学习框架。本书由Peter Marklund编写,包含了五天...
Ruby on Rails是一款基于Ruby语言的开源Web开发框架,它遵循MVC(模型-视图-控制器)架构模式,简化了Web应用的开发流程。在Linux环境下安装Ruby on Rails需要一系列的依赖包和步骤,本资源包提供了所需的所有组件,...
Ruby on Rails,简称RoR,是由David Heinemeier Hansson基于Ruby语言开发的一款开源Web应用程序框架,它遵循MVC(模型-视图-控制器)架构模式,旨在提高开发效率和可读性,使得开发者能够更快速地构建功能丰富的web...
Ruby on Rails,简称Rails,是基于Ruby编程语言的一个开源Web应用程序框架,它遵循MVC(模型-视图-控制器)架构模式,旨在提高开发效率和代码的可读性。Rails以其“约定优于配置”(Convention over Configuration)...
rails-dev-box, 面向 Ruby on Rails 核心开发的虚拟机 用于 Ruby on Rails 核心开发的虚拟机简介注意:这个虚拟机不是为 Rails 应用程序开发而设计的,只是为。 这个项目自动设置开发环境,以便在 Ruby on Rails ...
Ruby on Rails,简称Rails,是基于Ruby语言的一个开源Web应用程序框架,它遵循MVC(Model-View-Controller)架构模式,旨在使Web开发过程更加高效、简洁。本压缩包中的"Ruby on Rails入门经典代码"提供了新手学习...
《Ruby on Rails 3 Tutorial》是一本专门为初学者设计的指南,旨在帮助读者快速掌握Ruby on Rails这一强大的Web开发框架。Ruby on Rails(简称Rails)是基于Ruby语言的一个开源框架,它采用MVC(Model-View-...
Ruby on Rails,简称Rails,是一款基于Ruby语言的开源Web应用框架,它遵循MVC(Model-View-Controller)架构模式,旨在简化Web应用程序的开发。Rails由David Heinemeier Hansson于2004年创建,它提倡“约定优于配置...
Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zipRuby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zipRuby_on_...
Ruby on Rails与MongoDB 您可以在MongoDB的帮助下轻松... rails new ruby-on-rails-with-mongodb --skip-active-record从您的Gemfile中删除sqlite3(如果存在),将Mongoid添加到您的Gemfile中,然后运行“ bundle”。
### Ruby on Rails Cheat Sheet 本篇文章将从给定的文件中提炼出关于Ruby on Rails的重要知识点,主要包括命令、URL映射、命名规范、ERB标签、链接创建、数据库配置及查询、模型之间的关系等方面。 #### Ruby on ...
《Ruby on Rails for Dummies》是一本专门为初学者设计的Ruby on Rails教程,它旨在帮助新手快速理解并掌握这个强大的Web开发框架。Ruby on Rails(简称Rails)是基于Ruby编程语言构建的一个开源Web应用程序框架,它...