views:

452

answers:

3

I was reading Simply Rails by Patrick Lenz... maybe I missed something, it seems that whenever we put

map.resources :stories

in routes.rb

then immediately, the controller will have special convention and now Story is a RESTful resource? Maybe the author used the word resource but didn't mention that it is RESTful but they are the same thing?

+3  A: 

Having that in routes means that you automatically get some standard routes that help you build a restful application. For example:

 new_story GET     /story/new(.:format)  {:action=>"new", :controller=>"stories"}
edit_story GET     /story/edit(.:format) {:action=>"edit", :controller=>"stories"}
     story GET     /story(.:format)      {:action=>"show", :controller=>"stories"}
           PUT     /story(.:format)      {:action=>"update", :controller=>"stories"}
           DELETE  /story(.:format)      {:action=>"destroy", :controller=>"stories"}
           POST    /story(.:format)      {:action=>"create", :controller=>"stories"}

Just having this one line in your routes file, gives you all these paths to use. You just have to make sure you provide the right functionality in new, edit, show, update, destroy and create actions of your stories controller and you will have a restful design.

In order to see what is available route-wise, you can go to your application folder and give the command:

rake routes

This is going to output all the paths available to you, based on what you have entered in your routes file.

Petros
+1  A: 

Yes. Once you add that to your routes your Story controller will respond to the common REST verbs in the expected ways.

jrcalzada
+1  A: 

BUT!!! If you have other actions in your controller they will NOT be found unless you introduce additional routes ABOVE that .resources line!

So if you have an action called turn_page in the stories controller you need to include a map.connect line before the map.resources line - as in this snippet:

map.connect 'stories/turn_page', :controller => 'stories', :action => 'turn_page'
map.resources :stories

Hope that helps someone! I got stuck for hours working on this one as all the examples are EITHER "regular" routes OR the REST set defined via the .resources statement!

Mark