How to GET/POST in Rails ?

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 ?

-Arun

def action if request.post?

Do post stuff

elsif request.get?

Do get stuff

end end

This should be handled with Restful routes, but you said you didn’t want to do it that way.

Ok, that worked!

If I want only GET and POST to be handled in a particular manner, how would RESful routes be configured ?

Also, a newbie question ... how do I know where the list of variables like "request" accessible in my controller ?

-Arun

Arun wrote:

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.

Google returned lot of material but most of them is pre-Rails 2.0 or not well explained. Did I miss any obvious document ?

I don't think any of that will explain things in the terms you need!

I don't know if this is exactly what you are looking for but I highly reccomend reading through:

http://caboo.se/doc/classes/ActionController/Base.html

This is indeed a good article, bookmarked!

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

This is printing a very long string ...

GATEWAY_INTERFACECGI/1.1PATH_INFO/home/ indexQUERY_STRINGREMOTE_ADDR127.0.0.1REMOTE_HOST127.0.0.1REMOTE_USERREQUEST_METHODGET...

How do I extract a particular value from this hash ?

-Arun

> 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 ?

-Arun

You may want to look at this:

http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html

(api for request object)

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.