Rails:如何从 url 获取所有参数? [英] Rails: How to get all parameters from url?
本文介绍了Rails:如何从 url 获取所有参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常,我们使用像:
params[:a] #to get a specific parameter's value
但是如何像我们在 PHP 中那样获取所有参数呢?
But how to get all the parameters the way we do in PHP?
$_GET or $_POST
推荐答案
您可以简单地使用 params
作为所有传递参数(GET 和 POST)的哈希.
You may simply use params
as a Hash of all passed parameters (both GET and POST).
例如:
params.each do |key,value|
Rails.logger.warn "Param #{key}: #{value}"
end
更新:注意,params
包含哪些类别参数:
Update: Note, what params
include parameters of categories:
- 路径参数(绑定在路由中)
- 查询参数 (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
所有方法都返回HashWithIndifferentAccess,因此您可以通过字符串或符号访问它们键.
All methods return HashWithIndifferentAccess, so you may access them by string or symbol key.
这篇关于Rails:如何从 url 获取所有参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文