How to tell whether your controller action was given parameters?
I am trying to determine whether my controller action was called with parameters or not, without hardcoding which parameters can be added on.
So I want to distinguish between
/my_controller
and
/my_controller?q=1
I know that I could look inside the params hash, and check whether it ONLY contains :controller and :action keys. This seems ugly to me, is there a smarter way of doing this开发者_StackOverflow中文版 check?
There is one direct solution:
request.env["QUERY_STRING"] # => "q=1"
Or with Ruby 1.9.2:
request.env.QUERY_STRING # => "q=1"
For GET request you can use request.query_parameters
method. There is also request.request_parameters
for POST requests.
Results for request.query_parameters.inspect
are:
- for '/my_controller' => '{}'
- for '/my_controller?q=1' => {"q"=>"1"}
精彩评论