Rails:查询参数与发布参数 [英] Rails: query parameter vs post parameter

查看:60
本文介绍了Rails:查询参数与发布参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个网址

localhost:3000?a=1

并且在请求中,您还有一个 post 参数

and in the request, you also have a post parameter

a=2

会怎样

params[:a] 在这种情况下?是否取决于 HTTP 动词?

如果它确实依赖于 HTTP 动词,

If it does depend on the HTTP verb,

如果您的表单看起来像什么

what if your form looks like

<form method='post' action='/?a=2'>
  <input type='hidden' name='a' value='3'/>
</form>

在这种情况下 params[:a] 是什么?

what would params[:a] be in this case?

更新

所以我只是做了一个小实验,并使用 Chrome Debugger 附加 ?authenticity_token=abc 操作 url.我查看了服务器日志,看到参数有 authenticity_token =>'abc'.我也相信在这种情况下该方法是 POST.

So I just did a small experiment and used Chrome Debugger to append ?authenticity_token=abc the action url. I looked at the server log and I saw that parameters has authenticity_token => 'abc'. I also believe that the method is POST in this case.

让我知道你们想出了什么.

Let me know what you guys came up with.

推荐答案

当我在示例代码中尝试此操作时,我看到的是查询参数 (GET) 的优先级高于 POST 正文.因此,我深入研究了在 Rails 中处理 HTTP 请求的 Rack 代码.这是来自 request.rb

When I tried this in a sample code, what I was able to see was that query parameters(GET) are given precedence than the POST body. So, I went digging into the code of Rack which handles the HTTP requests in Rails. Here is the code from request.rb

# Returns the data recieved in the query string.
def GET
  ....
end

# Returns the data recieved in the request body.
#
# This method support both application/x-www-form-urlencoded and
# multipart/form-data.
def POST
  ....
end

# The union of GET and POST data.
def params
  @params ||= self.GET.merge(self.POST)
rescue EOFError
  self.GET
end

这里是方法

  • GET - 以散列格式返回查询参数
  • POST - 以散列格式返回帖子正文

因此,根据 params 的代码,如果键相同,GET 参数应该被 POST 参数覆盖.(self.GET.merge(self.POST)).但是,这与我实际尝试时得到的结果相反.

So, according to the code for params, the GET parameters should get overridden by POST parameters in case of identical keys. (self.GET.merge(self.POST)). But, this is contrary to what I got when I tried it practically.

所以,唯一的机会是这段代码被 Rails 覆盖了.当我想到它时,这是完全合理的,因为来自 Rails 的 params 哈希将始终包含 "controller""action" 键,在 Rack 的情况下将不存在.所以,我也查看了 Rails 的代码,发现 params 方法确实被覆盖了.看看 request.rb和 Rails 源代码中的 parameters.rb代码.在 parameters.rb 中,我们有:

So, the only chance is that this code is being overridden by Rails. When I thought about it, it made perfect sense as the params hash from the Rails will always contain "controller" and "action" keys, which will be absent in case of Rack. So, I looked at the code of Rails also, and found that params method was indeed being overridden. Take a look at request.rb and parameters.rb in Rails source code. In parameters.rb, we have:

  # Returns both GET and POST \parameters in a single hash.
  def parameters
    @env["action_dispatch.request.parameters"] ||= begin
      params = request_parameters.merge(query_parameters)
      params.merge!(path_parameters)
      encode_params(params).with_indifferent_access
    end
  end
  alias :params :parameters

request.rb:

# Override Rack's GET method to support indifferent access
def GET
  @env["action_dispatch.request.query_parameters"] ||= (normalize_parameters(super) || {})
end
alias :query_parameters :GET

# Override Rack's POST method to support indifferent access
def POST
  @env["action_dispatch.request.request_parameters"] ||= (normalize_parameters(super) || {})
end
alias :request_parameters :POST

所以,这里

  • query_parameters - GET 方法的别名
  • request_parameters - POST 方法的别名
  • path_parameters - 以散列形式返回请求的控制器和操作的方法
  • 参数 - params 的别名(此处已覆盖)
  • query_parameters - Alias for the GET method
  • request_parameters - Alias for the POST method
  • path_parameters - Method which returns the controller and action for the request as a hash
  • parameters - Alias for params (It was overridden here)

注意GET方法和POST方法也被覆盖了,主要是将返回的hash转换为HashWithIndifferentAccess的一个对象.

Note that GET method and POST method were also overridden, mainly to convert the hash returned to an object of HashWithIndifferentAccess.

因此,查看此处的代码 (params = request_parameters.merge(query_parameters)),很明显,在 Rails 中,如果键相同,POST 参数会被 GET 参数覆盖.或者换句话说,GET 参数优先于 POST 参数.

So, looking at the code here (params = request_parameters.merge(query_parameters)), it becomes evident that POST parameters are overridden by GET parameters in case of identical keys, in Rails. Or in other words, GET parameters are given precedence over POST parameters.

这篇关于Rails:查询参数与发布参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆