วันอาทิตย์ที่ 23 ตุลาคม พ.ศ. 2554

Ruby on Rails - Rails Routing

The topic I'm writing today is about how we map URLs to corresponding resources by using Rails Routing feature. The Rails Router is a component that manage this. It recognizes URLs and dispatches them to a controller's action.

For example, when you enter http://localhost:3000/quotes/1 to the address bar of the web browser and in the config/routes.rb file contain the following line:

match "/quotes/:id" => "quotes#show"

The Router will dispatch /quotes/1 to the quote controller's show action with { :id => 1 } in params. Also, we can generate a link to a particular quote by using the link_to helper; the information in the routes.rb will be used in generating the link as the example shown below.

<%= link_to "Link to this page", quote_path(@quote)%>

If we put the above code in the show.html.erb file for the quote, the result is as the following screenshot:


In addition, the RoR also provide the resource routing, it allows us to declare all of the common routes for a given resourceful controller. Browsers request pages from Rails by making HTTP requests with a specific HTTP method; GET, POST, PUT and DELETE. Each method is for performing an operation on the requested resource. A resource route will map a specified HTTP method to corresponding action in a single controller.

Let's assume that we have the resources declaration for the Quote controller

resources: quotes

and a browser make the HTTP request

DELETE /quotes/2

When Rails receives the HTTP request, it will map the request to the destroy method of the QuotesController as shown below.

  def destroy
    @quote = Quote.find(params[:id])
    @quote.destroy


    respond_to do |format|
      format.html { redirect_to quotes_url }
      format.json { head :ok }
    end
  end 

The HTTP methods or verbs along with requested URLs will be mapped to corresponding actions of a controller. Referring to the resources declaration above all the HTTP verbs and corresponding controller's actions as shown below.

HTTP Verb
Path
Action
Description
GET
/quotes
index      
Show a list of all quotes
GET
/quotes/1
show
Show the detail of the quote (ID 1)
GET
/quotes/new
new
Show the form for creating a new quote
POST
/quotes
create
Create a new quote
GET
/quotes/1/edit    
edit
Show the form for modifying the quote (ID 1)
PUT
/quotes/1
update
Update the quote (ID 1)
DELETE           
/quotes/1
destroy      
Delete the quote (ID 1)

Rails provides a number of the helpers for generating URLs that are mapped to the controller's actions such as:
  • quotes_path returns /quotes
  • new_quotes_path returns /quotes/new
  • quote_path returns /quotes/:id, where :id is a quote ID
The resource routing for a singular resource is supported by using the match, as I shown earlier, or give the resources declaration a singular resource, such as:

resources :quote

Rails will map the HTTP verbs and URLs to the QuotesController's actions as follows:

HTTP Verb
Path
Action
Description
GET
/quote
show        
Show the detail of the quote
GET
/quote/new   
new
Show the form for creating a new quote
POST
/quote
create
Create a new quote
GET
/quote/edit
edit
Show the form for modifying the quote
PUT
/quote
update
Update the quote
DELETE           
/quote
destroy      
Delete the quote

Also, the resource routing supports the namespace and nested resources, you can find more information at this link, http://guides.rubyonrails.org/routing.html.

In this post, I would like to give the basic idea of the resource routing. When I use the advance features, I will post more about them later.

Have a nice week end :D.


ไม่มีความคิดเห็น:

แสดงความคิดเห็น