Identify GET and POST parameters in Ruby on Rails

前端 未结 11 784
我在风中等你
我在风中等你 2020-12-08 14:16

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

11条回答
  •  忘掉有多难
    2020-12-08 15:02

    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.

提交回复
热议问题