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?
I believe it's specified by REST. Here's a list for ya:
Edited to add to get all those routes, in config/routes.rb, simply add
map.resources :items
The best place to learn about this would be the Routing Guide.
This will help a lot, but it's not a direct answer to your question. The following command will list the mappings your app uses so you don't have to remember all the details or guess.
To answer more directly, this is a convention that rails uses. You set this mapping up when you put something like the following in your routes.rb
To be honest, you can't really go wrong with the routing documentation on the Rails website. This has helped take the next steps and move beyond the comfort of resources (which for most apps is fine)and really nail down the solid routing features available.
http://guides.rubyonrails.org/routing.html
Did you want to know how to use POST only? Do this, for example:
..etc. This is for Rails 3 by the way, and will generate a single resource to POST create. Or if you only need a really small subset of the REST set, just:
etc etc.
Like Don Werve said, take a look at your routes.rb file. In there you probably have something like this:
This is where rails links the POST and GET requests to certain actions. To see how this works look at the links from the other answers. The docs help a ton.
To all the routes and which actions they link to you can type
rake routes
into the command prompt when you are in the root of your rails directory. This will show you everything (in terms of routing) that a scaffold gives you.