views:

9744

answers:

5

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?

+13  A: 

You can use the get? and post? methods to distinguish between HTTP Gets and Posts.

John Topley
The same link details how to get the parameters. That is what he is after.
Till
They've changed site structure, so now this is the correct link: http://api.rubyonrails.org/classes/ActionDispatch/Request.html
Nikita Rybak
+1  A: 

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.

Toby Hede
That's not just a Rails thing - all well-behaved web applications should do that.
John Topley
i do not understand actually. what is wrong with checking the request type, and getting the right set of params?
ming yeow
@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
+6  A: 

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
Ben Scofield
Wow, why the downvotes? This answer solves the stated problem (unlike most of the other).
Ben Scofield
This is over the top; get?, post? or even request_method are much better options for determining the request type.
MatthewFord
but... 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
+5  A: 

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.

webmat
John Topley
I think you're right. I went from memory ;-)
webmat
+3  A: 

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.

domgblackwell