开发者

How to tell whether your controller action was given parameters?

开发者 https://www.devze.com 2023-04-05 16:08 出处:网络
I am trying to determine whether my controller action was called with parameters or not, without hardcoding which parameters can be added on.

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"}
0

精彩评论

暂无评论...
验证码 换一张
取 消