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
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Ruby on Rails Routing - Simple Examples

  • Ruby Symbol

    Ruby is a completely cross-platform object-oriented scripting language, built on the Ruby-based Web framework Ruby on Rails (ROR) is the field of web development is gaining increasing acceptance. ROR substantial use of the Ruby language features, Symbol i

  • In the Linux platform to install and configure Ruby on Rails Detailed

    ruby on rails recommend the production environment is running Linux / FreeBSD / Unix, or Unix family of operating systems, using lighttpd + FCGI solution. The following will be my Linux operating system, lighttpd + FCGI, MySQL database as an example, comp

  • In the Windows platform using Apache2.2 and Mongrel running Ruby on Rails

    First, install Ruby, rails, mongrel and Apache2.2 Rubyforge download from the web site One-Click Ruby Install, run setup on installed ruby and rubygems. Run the command: gem install rails-y gem install mongrel-y gem install mongrel_service-y Installed rai

  • jdbc even ORACLE, SQLServer2000, mysql ways

    According to other people say can make a ride to one of the clear step by step, not as easy to write your own ideas! 1. Even the oracle The direct use of the oracle are provided give a jar package jdbc: oracle alone is installed, and 10g of oracle on ...

  • Rails source code analysis (1): RailsFCGIHandler

    In accordance with the sequence starting from the beginning CGI Ruby CGI Doc: The Common Gateway Interface ( CGI ) Is a simple protocol for passing an HTTP request from a web server to a standalone program, and returning the output to the web browser ...

  • Using Rails Captcha plug-in, easy implementation Verification Code

    Simple Captcha Plugin Can help us easily in the Rails implementation of Verification Code function. In addition, he has the option to provide sufficient to meet the U.S. requirements for certification, the use of easy. Supported picture and digital authen

  • Pyronaridine listing! Ruby programming language

    Interactive Net Purchase Excellence Amazon purchase [Original title] The Ruby Programming Language Publishing ] [ O'Reilly Media, Inc. [Author] (United States) David Flanagan; Yukihiro Matsumoto Translator [] LIAO Zhi-gang; Zhang Wo [Name] Books O

  • Rails command

    In windowsxp install ruby on rails of sqlite3-ruby problem-solving nmake 'nmake' is not recognized as an internal or external command,

  • Hibernate Inteceptor

    The end of the project stage, the client suddenly put forward a very troublesome but normal demand, the system records all changes must be carried out. Formats such as: 2004.1.1 12:30 Ikuya wind orders Sales Order Date 2004.1.2-> 2004.1.3 The firs ...

  • RoR explained

    ROR is Ruby on Rails. Ruby is a well-known has been very good dynamic language It's dynamic language. Simple and easy. Dynamic languages are interpreted, but the performance may make a discount, but not absolute, because the application is complex, th

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries