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.
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
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.
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
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.
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
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
Routes can generate pretty URLs, for example see following
Ruby on Rails 2.1 Related Interview Questions
|Python Interview Questions||HTML 5 Interview Questions|
|PHP Interview Questions||PostgreSQL Interview Questions|
|Ruby Interview Questions||Django Interview Questions|
|wxPython Interview Questions||MongoDB Interview Questions|
|Python Automation Testing Interview Questions||GitHub Interview Questions|
|Ruby Sinatra Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.