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 ?
Basically covers the basics of what you can do in a controller. From the
document:
Requests are processed by the Action Controller framework by extracting
the value of the “action” key in the request parameters. This value
should hold the name of the action to be performed. Once the action has
been identified, the remaining request parameters, the session (if one
is available), and the full request with all the http headers are made
available to the action through instance variables. Then the action is
performed.
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
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
Is there a definitive guide on how to create a controller that
understands and responds to GET/POST methods ?
Someone else here might understand your question better than me, but…
…that’s all controllers ever do! They provide actions that respond to GET
and POST HTTP events, routed in from a web server.
How do controllers figure out which actions are bound to GET and
POST ?
Is it explicitly configured in routes or a marker or something else ?
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.