# How to GET/POST in Rails?

**URL:** <https://www.ruby-forum.com/t/how-to-get-post-in-rails/135437>\
**Category:** Rails\
**Created:** [April 22, 2008, 7:26am UTC](https://www.ruby-forum.com/t/how-to-get-post-in-rails/135437 "2008-04-22T07:26:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![arun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@arun](https://www.ruby-forum.com/u/arun)\
**Post date:** [April 22, 2008, 7:26am UTC](https://www.ruby-forum.com/t/how-to-get-post-in-rails/135437/1 "2008-04-22T07:26:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![arun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@arun](https://www.ruby-forum.com/u/arun)\
**Post date:** [April 22, 2008, 7:28am UTC](https://www.ruby-forum.com/t/how-to-get-post-in-rails/135437/2 "2008-04-22T07:28:08Z")

</div>

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.

On Tue, Apr 22, 2008 at 2:55 PM, Arun [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![arun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@arun](https://www.ruby-forum.com/u/arun)\
**Post date:** [April 22, 2008, 7:44am UTC](https://www.ruby-forum.com/t/how-to-get-post-in-rails/135437/3 "2008-04-22T07:44:35Z")

</div>

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

On Apr 21, 10:27 pm, “Ryan B. (Radar)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![arun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@arun](https://www.ruby-forum.com/u/arun)\
**Post date:** [April 22, 2008, 8:06am UTC](https://www.ruby-forum.com/t/how-to-get-post-in-rails/135437/4 "2008-04-22T08:06:50Z")

</div>

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](http://caboo.se/doc/classes/ActionController/Base.html)

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

---

<div class="post-metadata">

**Author:** ![arun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@arun](https://www.ruby-forum.com/u/arun)\
**Post date:** [April 22, 2008, 1:08pm UTC](https://www.ruby-forum.com/t/how-to-get-post-in-rails/135437/5 "2008-04-22T13:08:19Z")

</div>

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!

–  
Phlip

---

<div class="post-metadata">

**Author:** ![arun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@arun](https://www.ruby-forum.com/u/arun)\
**Post date:** [April 22, 2008, 8:09pm UTC](https://www.ruby-forum.com/t/how-to-get-post-in-rails/135437/6 "2008-04-22T20:09:31Z")

</div>

> 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](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

---

<div class="post-metadata">

**Author:** ![arun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@arun](https://www.ruby-forum.com/u/arun)\
**Post date:** [April 22, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/how-to-get-post-in-rails/135437/7 "2008-04-22T20:10:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![arun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@arun](https://www.ruby-forum.com/u/arun)\
**Post date:** [April 22, 2008, 11:59pm UTC](https://www.ruby-forum.com/t/how-to-get-post-in-rails/135437/8 "2008-04-22T23:59:28Z")

</div>

You may want to look at this:

[http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html](http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html)

(api for request object)

---

<div class="post-metadata">

**Author:** ![arun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@arun](https://www.ruby-forum.com/u/arun)\
**Post date:** [April 23, 2008, 1:44am UTC](https://www.ruby-forum.com/t/how-to-get-post-in-rails/135437/9 "2008-04-23T01:44:04Z")

</div>

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