O'Reilly logo

Ruby on Rails® for Microsoft Developers by Antonio Cangiano

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8.3. RESTful Routes

REST (REpresentational State Transfer) was already introduced earlier in the book when you developed the basic blog application. This section briefly recaps how this new paradigm affects Rails' routing.

RESTful routing is considered to be the new standard for Rails applications and, whenever possible, it should be favored over the traditional style described so far. In short, RESTful routing doesn't simply match a URL to code within the controller, but rather maps resource identifiers (think URLs) and HTTP verbs to seven predefined actions. These actions normally perform CRUD operations in the database as well, through the model layer.

8.3.1. map.resources

RESTful routes can be defined through the resources method. Consider this route:

map.resources :books

Whenever you need to declare more than one resource, you can do so on a single line by passing a list of symbols (for example, map.resources :books, :catalogs, :users) to resources.

This manages to pull off a fair bit of magic, by abstracting and hiding many of REST's implementation details, as well as providing you with named routes and easy-to-use helpers to work with. A single concise line creates seven RESTful routes, as shown in the following table.

Route NameHTTP MethodURLAction in BooksController
booksGET/booksindex
formatted_booksPOST/bookscreate
new_bookGET/books/newnew
bookGET/books/:idshow
edit_bookGET/books/:id/editedit
formatted_bookPUT/books/:idupdate
formatted_bookDELETE/books/:iddestroy

Each named ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required