Section 5. RESTful Routes

5.1 Pretty URLs 525.2 Browsers and the HTTP Methods 53

In many Rails projects, routes.rb is one of the meanest, nastiest sections of code. Controllers get renamed in URLs and a million little named routes are created to assist in view creation. Resources can be used to partially clean up the routes.rb mess. We can establish a controller as a resource using map.resource. It also gives us method invocation via a URL and HTTP method as discussed in detail in Section 4. For our tasks controller from Section 4 we add the following simple route. map.resources :tasks

That’s it. Now we can access our tasks at /tasks and /tasks/1, along with the appropriate HTTP method.

With that out of the way, we examine the various options ...

Get Rails Refactoring to Resources: Using CRUD and REST in Your Rails Application now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.