Is there a definitive guide on how to create a controller that
understands and responds to GET/POST methods ? This does not
necessarily mean I'm trying RESTful Web services.
Google returned lot of material but most of them is pre-Rails 2.0 or
not well explained. Did I miss any obvious document ?
The full request object is available with the request accessor and is
primarily used to query for http headers. These queries are made by
accessing the environment hash, like this:
def server_ip
location = request.env["SERVER_ADDR"]
render :text => "This server hosted at #{location}"
end
How do controllers figure out which actions are bound to GET and
POST ?
This is a portion of the routes defined in one of my applications:
tickets GET /tickets
{:action=>"index", :controller=>"tickets"}
POST /tickets
{:action=>"create", :controller=>"tickets"}
new_ticket GET /tickets/new
{:action=>"new", :controller=>"tickets"}
edit_ticket GET /tickets/:id/edit
{:action=>"edit", :controller=>"tickets"}
ticket GET /tickets/:id
{:action=>"show", :controller=>"tickets"}
PUT /tickets/:id
{:action=>"update", :controller=>"tickets"}
DELETE /tickets/:id
{:action=>"destroy", :controller=>"tickets"}
Here is a sample link in a view:
<%= link_to "Tickets", tickets_path %>
The "tickets_path" helper method inside the link maps to this route:
POST /tickets
{:action=>"create", :controller=>"tickets"}
Which of course then maps to the "create" action of the "tickets"
controller.
If you check the development log after clicking the link you would
find this line:
Parameters: {"action"=>"create", "controller"=>"tickets"}
Note: There is probably some other values in the parms hash, but these
two are used for selecting the controller and action.
As far as the method used for the request (GET/POST/PUT/DELETE/HEAD)
the request object knows that, which Rails has access to (and so do
you).
Hope this helps clear up some of the voodoo magic.