What is the simplest way to identify and separate GET and POST parameters from a controller in Ruby on Rails, which will be equivalent to $_GET and $_POST variables in PHP?
-
3I think the question is more 'given a request which has both POST data, and a query string' (which is entirely likely), how can you tell which parameters came from where? - Is this right? – Orion Edwards Oct 01 '08 at 01:04
-
I don't think he means he wants to separate the GET params from the POST params, but that the wants to get each parameter separately, whether it be GET or POST. He basically wants the Rails equivalent to $_GET and $_POST from PHP. – iconoclast Jun 19 '11 at 22:38
-
@Brandon: Actually, I did mean what Orion Edwards said, but that was a long time ago. I now consider this a very stupid question. Albeit given the amount of views its still getting, many have not understood what I hadn't understood then. – Swanand Jun 21 '11 at 14:47
-
then wouldn't Ben Scofield's answer be more appropriate to the question as you intended it? Why isn't his the accepted answer? – iconoclast Jun 24 '11 at 03:12
11 Answers
You can use the request.get?
and request.post?
methods to distinguish between HTTP Gets and Posts.

- 611
- 2
- 11
- 27

- 113,588
- 46
- 195
- 237
-
1The same link details how to get the parameters. That is what he is after. – Till Sep 30 '08 at 12:21
-
8They've changed site structure, so now this is the correct link: http://api.rubyonrails.org/classes/ActionDispatch/Request.html – Nikita Rybak Sep 05 '10 at 23:58
-
1these methods are on the request. request.get? and request.post? – Dimitris Baltas Dec 22 '11 at 23:26
I don't know of any convenience methods in Rails for this, but you can access the querystring directly to parse out parameters that are set there. Something like the following:
request.query_string.split(/&/).inject({}) do |hash, setting|
key, val = setting.split(/=/)
hash[key.to_sym] = val
hash
end

- 6,398
- 2
- 23
- 22
-
1Wow, why the downvotes? This answer solves the stated problem (unlike most of the other). – Ben Scofield Oct 01 '08 at 16:44
-
3This is over the top; get?, post? or even request_method are much better options for determining the request type. – MatthewFord Oct 02 '08 at 23:42
-
6but... the question wasn't to determine the request type, and that isn't even close to what this code does. As I understood the question, it was (paraphrasing) "how do I retrieve parameters from get and post separately?" Guess I'm just way off the mark with my reading comprehension... – Ben Scofield Oct 03 '08 at 12:09
-
From what Swanand (the asker) said to me above, it seems that this is really the best answer, and the down-voters should take back their downvotes. – iconoclast Jun 24 '11 at 03:14
-
I agree, and I changed the accepted answer. I should have done it sooner, but this was asked so long ago that it went out of reckoning. – Swanand Jun 25 '11 at 07:45
-
@Ben Scofield: Thanks for the answer. +1 and enjoy your 'Nice Answer' badge. :-) – Swanand Jun 27 '11 at 14:58
-
I initially downvoted this, but took it back. The real question here is: Why in the world do you care if the parameters are GET or POST parameters? If you only support one or the other, alter your routes to only allow one or the other. If you're passing in both GET and POST parameters, you're doing it wrong. – ZiggyTheHamster Aug 07 '12 at 21:55
-
2Downvotes are anonymous by design and no one is *entitled* to an explanation. Best thing to do is shrug it off and move on. – BryanH Feb 26 '13 at 13:20
You can do it using:
request.POST
and
request.GET

- 45,245
- 23
- 243
- 245

- 1,010
- 9
- 16
There are three very-lightly-documented hash accessors on the request object for this:
request.query_parameters
- sent as part of the query string, i.e. after a ?request.path_parameters
- decoded from the URL via routing, i.e. controller, action, idrequest.request_parameters
- All params, including above as well as any sent as part of the POST body
You can use Hash#reject
to get to the POST-only params as needed.
Source: http://guides.rubyonrails.org/v2.3.8/action_controller_overview.html section 9.1.1
I looked in an old Rails 1.2.6 app and these accessors existed back then as well.

- 313
- 3
- 12
There is a difference between GET and POST params. A POST HTTP request can still have GET params.
GET parameters are URL query parameters.
POST parameters are parameters in the body of the HTTP request.
you can access these separately from the request.GET and request.POST hashes.

- 4,509
- 1
- 20
- 19
request.get?
will return boolean true if it is GET method,
request.post?
will return boolean true if it is POST method,

- 636
- 8
- 19
If you want to check the type of request in order to prevent doing anything when the wrong method is used, be aware that you can also specify it in your routes.rb file:
map.connect '/posts/:post_id', :controller => 'posts', :action => 'update', :conditions => {:method => :post}
or
map.resources :posts, :conditions => {:method => :post}
Your PostsController's update method will now only be called when you effectively had a post. Check out the doc for resources.

- 58,466
- 12
- 54
- 59
-
You can also use :verify :method => :post etc. in a before filter. Won't your second example break the index, new, edit, update, show & destroy actions in a RESTful controller? – John Topley Sep 30 '08 at 12:51
I think what you want to do isn't very "Rails", if you know what I mean. Your GET requests should be idempotent - you should be able to issue the same GET request many times and get the same result each time.

- 36,755
- 28
- 133
- 162
-
4That's not just a Rails thing - all well-behaved web applications should do that. – John Topley Sep 30 '08 at 11:14
-
i do not understand actually. what is wrong with checking the request type, and getting the right set of params? – meow Jul 14 '10 at 09:46
-
1@mingyeow - Rails lets you ensure that certain actions respond to specific HTTP methods. In terms of overall application design this is a much better strategy than having parameters handled according to their origin. – Toby Hede Jul 14 '10 at 12:37
You don't need to know that level of detail in the controller. Your routes and forms will cause appropriate items to be added to the params hash. Then in the controller you just access say params[:foo]
to get the foo parameter and do whatever you need to with it.
The mapping between GET and POST (and PUT and DELETE) and controller actions is set up in config/routes.rb
in most modern Rails code.

- 5,042
- 1
- 19
- 11
I think what Jesse Reiss is talking about is a situation where in your routes.rb
file you have
post 'ctrllr/:a/:b' => 'ctrllr#an_action'
and you POST to "/ctrllr/foo/bar?a=not_foo" POST values {'a' => 'still_not_foo'}, you will have three different values of 'a': 'foo', 'not_foo', and 'still_not_foo'
'params' in the controller will have 'a' set to 'foo'. To find 'a' set to 'not_foo' and 'still_not_foo', you need to examine request.GET
and request.POST
I wrote a gem which distinguishes between these different key=>value pairs at https://github.com/pdxrod/routesfordummies.

- 20,354
- 4
- 60
- 103