Ruby on Rails 2.1 Routes System - Ruby on Rails 2.1

How Routing system in Ruby on Rails 2.1 works?

Routing works in Rails as same as on any web server. With Rails routing, the whole routing process will be under users control and parts of URL specifies additional parameters. Rails parse, URL to verify controller, action, and parameters for the request

Rails use’s file config/routes.rb to handle Routing system and it is centre of the Rails routing system. File contains rules, which tries to match the URL path of a request and decide where to direct that request.

The rules are tested in order as they're defined in the file. The first rule matches the request's URL path and second determines the fate of that request.

Routing system essentially does two this while routing a request.

  • Maps requests to action methods inside the controllers.
  • Writes URLs for user to use as arguments to methods like link_to, redirect_to, and form_tag.

Now, Routing system knows how to turn a visitor's request URL into a controller/action sequence and how to manufacture URL strings based on specifications.

Follow the below route, installed by Rails when user generates an application

The route state that it expects requests, to consist of a :controller followed by an :action that in turn is fed some :id.

It maps as below, if user gets an incoming request for http://localhost:3000/book/edit/2

And the default routing (if no modification in the routing rules) is

If URL as http://www.example.com/book/update/20, calls the updatemethod (the action) in the BooksController class (the controller) with an idparameter set to the value 20.

Below, would set up book as the default controller, if none is specified, which means visiting '/' would invoke the book controller.

User can define a default action if no action is specified in the given URL

User can edit using ‘edit method’’ inside the book controller to edit book with ID as 20 as follows

Route priority

Routing system sets priority, by defining the order of appearance of the routes in the routes.rb file, which would be set as top to bottom.

Last request entered in the route file would have the lowest priority. If no route matches, 404 is returned.

Modifying the Default Route

User can set default route, as required.

Following example, how to interchange controller and action

To call action from the given controller user needs to write URL as follows

It's not mandatory to put action and controller in such sequence. The original default (the default default) route is better and recommended.

The Ante-Default Route

The 'ante-default' route looks like below

The.:format at the end matches a literal dot and a wildcard "format" value after the id field, which means it will match, for example, a URL like below

Here, inside the controller action your params[:format] will be set to xml.

The Empty Route

Empty route is kind of opposite of the default route.

The empty route is commented out in a recently generated routes.rb file, as there’s no universal or reasonable default for it. User has to define the use of this nothing URL should do for each application user writes.

Below are some examples of common empty route rules

And the explanation of the above rules is

  • First one will search for welcome action inside main controller even if user type just http://localhost:3000
  • Second one will connect to http://localhost:3000/main/index. Hereindex is the default action when there's none specified.

Rails 2.0 introduced, a mapper method called root, which is the right way to define the empty route for a Rails application, check below

Defining the empty route gives people something to look at when they connect to user’s site with nothing but the domain name.

Named routes

One continues developing application, will probably have a few links that are used throughout an application.

Considering following example, one would possibly often be putting a link back to the main listings page.

By using following line throughout application, user can instead create a named route that enables to link to a shorthand version of that link

##STARt##

link_to 'Home', :controller => 'classified', :action => 'list'

User can define named routes as follows. Use a unique name and define the route instead of using connect. Such case, the route is called home. All the routes look similar to one to others which have created.

User can use this in the controllers or views as following

Create a name for the route followed by _url to link, until now we used listing the :controller and :action method to link.

user shouldn't notice any difference. Named routing is a easy way for the Rails developer to save time from typing. Above case can be written without named route as follow

Pretty URLs

Routes can generate pretty URLs, for example see following


All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Ruby on Rails 2.1 Topics