Ruby-on-rails Rails:如何从 url 获取所有参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4352839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:47:55  来源:igfitidea点击:

Rails: How to get all parameters from url?

ruby-on-railsruby-on-rails-3

提问by mlzboy

Usually, we use like:

通常,我们使用像:

 params[:a] #to get a specific parameter's value

But how to get all the parameters the way we do in PHP?

但是如何像我们在 PHP 中那样获取所有参数呢?

  $_GET or $_POST

回答by alno

You may simply use paramsas a Hash of all passed parameters (both GET and POST).

您可以简单地将params所有传递的参数(GET 和 POST)用作散列。

For example:

例如:

params.each do |key,value|
  Rails.logger.warn "Param #{key}: #{value}"
end

Update: Note, what paramsinclude parameters of categories:

更新:注意,params包括类别参数的内容:

  • Path parameters (bound in routes)
  • Query parameters (GET)
  • Request parameters (POST)
  • 路径参数(绑定在路由中)
  • 查询参数 (GET)
  • 请求参数 (POST)

If you want to access parameters of certain category only you may use:

如果您只想访问某些类别的参数,您可以使用:

request.path_parameters

request.query_parameters # or
request.GET

request.request_parameters # or
request.POST

All methods return HashWithIndifferentAccess, so you may access them by string or symbol key.

所有方法都返回HashWithIndifferentAccess,因此您可以通过字符串或符号键访问它们。