After I generate a scaffold, Rails gives me the ability to POST to items.xml
which will create a new item
. A GET to items.xml
will simply list them all. Where does Rails specify which method in the controller (create
or index
, respectively) will be called, based on the type of action I am performing?
More specifically, POST calls methodA but GET to the same URL calls methodB. Where is this specified? Where does Rails make the determination to call the index
method of the controller?
Rails defines seven controller methods for RESTful resources by convention. They are:
Note that because web browsers generally only support GET and POST, Rails uses a hidden field to turn these into PUT and DELETE requests as appropriate.
Specifying
map.resources :items
inconfig/routes.rb
gets you those seven methods "for free". You can list all the routes within your application at any time by enteringrake routes
in the console.map.resources is a method that automagically gives you the REST routes and path helpers as well. This is a nice feature if you already know and understand how rails' restful routing works but it is also a bit of a hindrance for learning rails because too much is hidden.
Railsguides has a nice routes guide.