27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

First steps<br />

using the resources method, putting it directly under the root method in this file, as<br />

shown in the following listing.<br />

Listing 3.11 config/routes.rb<br />

resources :projects<br />

This is called a resource route, and it defines the routes to the seven RESTful actions in<br />

your projects controller. When something is said to be RESTful, it means it conforms<br />

to the Representational State Transfer (REST) standard. In Rails, this means the<br />

related controller has seven actions:<br />

� index<br />

� show<br />

� new<br />

� create<br />

� edit<br />

� update<br />

� destroy<br />

These seven actions match to just four request paths:<br />

� /projects<br />

� /projects/new<br />

� /projects/:id<br />

� /projects/:id/edit<br />

How can four be equal to seven? It can’t! Not in this world, anyway. Rails will determine<br />

what action to route to on the basis of the HTTP method of the requests to these<br />

paths. Table 3.1 lists the routes, HTTP methods, and corresponding actions to make it<br />

clearer.<br />

The routes listed in the table are provided when you use resources :projects.<br />

This is yet another great example of how Rails takes care of the configuration so you<br />

can take care of the coding.<br />

Table 3.1 RESTful routing matchup<br />

HTTP method Route Action<br />

GET /projects index<br />

POST /projects create<br />

GET /projects/new new<br />

GET /projects/:id show<br />

PUT /projects/:id update<br />

DELETE /projects/:id destroy<br />

GET /projects/:id/edit edit<br />

61

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!