简体   繁体   English

Rails:如何从url获取所有参数?

[英]Rails: How to get all parameters from url?

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

You may simply use params as 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 params include parameters of categories: 更新:注意,什么params包括类别的参数:

  • Path parameters (bound in routes) 路径参数(绑定在路由中)
  • Query parameters (GET) 查询参数(GET)
  • Request parameters (POST) 请求参数(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 ,因此您可以通过字符串或符号键访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM