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). Eg 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 eg / articles/2007/1)
articlearchiveurl () (generate absolute url eg 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 时间:2009-02-27 人气:2574
分享到:
blog comments powered by Disqus

相关文章

  • Ruby on Rails and to use Action Web Service to create Web Services 2009-05-25

    Action Web Service Module will be at Ruby on Rails implementation of Web services. Action Web Service will be the creation of SOAP and XML-RPC Web Services in support of server-side protocol. You can use the Declaration and the Action Web Service rel

  • Introduced the Ruby On Rails 2009-11-10

    === To http://kiwi.csie.chu.edu.tw/blog/archives/133 This article from the PHP and Web programmers point of view, and gradually bring out the Ruby on Rails and Web Application Framework of the advantages of the concept structure. Next Introduction to

  • Ruby on Rails Small Web site Performance Optimization step 2010-05-13

    Geek Express is my way by agile developers out a few days out of a RoR site, on-line a month, although the middle and gradually have to add some small features and improvements, but the speed and performance has been not too much to consider, so far

  • Non-computer professional learning ruby on rails Zatan 2010-11-10

    A company's internal communications, and more for non-computer areas of expertise, eyes of the beholder, the master laughed. For the non computer professional technical people, and sometimes, if you want to enjoy the fun of programming, ruby and rail

  • Ruby on Rails Tutorial (Michael Hartl) Chinese translation of Chapter II demo (1) 2010-12-14

    (Iseesea translation, reprint leave ~ Thank you ~ ~ have to support it adhere to the ~) Based Rails3.0.3 I try to translate the book, or need any objection can see the original. . . Quack. Pleased to meet you ~ deficiency. I learn while translation o

  • Ruby on Rails Tutorial (Michael Hartl) Chinese translation of Chapter II demo (3) 2010-12-16

    (Connected to the articles, just over half of the second chapter, fueling ~ on Rails article about a step in the use of MVC) I'm from the browser (such as IE) URL address bar or click on a link out of a request start, the request will first be reques

  • Ruby on Rails Tutorial (Michael Hartl) Chinese translation of static pages of Chapter III (2) 2010-12-22

    3.1.1 Let the real static pages from the static pages really start learning, review Section 1.2.5 on the start of each program on Rails is a very small program (mainly the Rails script credit, such as generator ) and have a default welcome page (addr

  • Windows下Ruby on Rails开发环境安装配置图文教程 2013-10-02

    这篇文章主要介绍了Windows下Ruby on Rails开发环境安装配置图文教程,ROR初学者必看,需要的朋友可以参考下 本文详细介绍如何在Windows配置Ruby on Rails 开发环境,希望对ROR初学者能有帮助. 一.下载并安装Ruby Windows下安装Ruby最好选择 RubyInstaller(一键安装包). 下载地址: http://rubyforge.org/frs/?group_id=167 . 我们这里下载目前较新的rubyinstaller-1.9.3-p0.e

  • Twitter没有计划放弃ruby on rails,而且早就用了erlang改进性能 2014-02-06

    Twitter 是用 RoR 开发的流量最大的站点. 应用了 Rinda,其基于 DRb使用DRb ("Distributed Ruby".), 该库可以通过 TCP/IP 从远程 Ruby 对象发送接收消息, ,还用到了erlang技术中著名的 ejabberd. 有消息称:Twitter计划放弃ruby on rails 5月9日,其官方网站上登信息辟谣, Twitter没有计划放弃ruby on rails,早就开始使用了混合语言改进性能. 为什么这个谣言传得这么快,这么像真的?

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.048 (s). 14 q(s)