Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify GET and POST parameters in Ruby on Rails

People also ask

What is params ID in Rails?

params[:id] is meant to be the string that uniquely identifies a (RESTful) resource within your Rails application. It is found in the URL after the resource's name.

How does params work in Rails?

As you might have guessed, params is an alias for the parameters method. params comes from ActionController::Base, which is accessed by your application via ApplicationController. Specifically, params refers to the parameters being passed to the controller via a GET or POST request.


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

  • See http://api.rubyonrails.org/classes/ActionDispatch/Request.html

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

You can do it using:

request.POST

and

request.GET


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, id
  • request.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.