Wraps the parameters hash into a nested hash. This will allow clients to submit POST requests without having to specify any root elements.
它有助于省略包装哪些参数哈希. Action Controller overview guide给出了这个破败:
Rails collects all of the parameters sent along with the request in the
params
hash,whether they are sent as part of the query string or the post body. […] Thequery_parameters
hash contains parameters that were sent as part of the query string while therequest_parameters
hash contains parameters sent as part of the post body. Thepath_parameters
hash contains parameters that were recognized by the routing as being part of the path leading to this particular controller and action.
当您使用RESTful资源和路由时,会发生这种乐趣.假设你有一个模型A has_many Bs;因此B具有外键a_id.
你使用空载荷POST / as / 1 / bs(因为B没有其他字段).假设a_id是attr_accessible,可以假设a_id将包装在b对象中.相反,你会看到:
Processing by BsController#create as HTML Parameters: {"b"=>{},"a_id" => "1"}
没有这样的运气.事实证明,ParamsWrapper
uses request_parameters
而不是params,因此在POST有效载荷中不包括a_id意味着它不会被包裹.这是非常令人困惑的,因为你仍然看到它包含在params中,由于URI globbing,并且想知道为什么它被排除在所有东西之外.
有没有什么好的理由在这里使用request_parameters而不是params?
我可以理解,从“REST哲学”的角度来看,如果我们假设有效载荷包含整个对象,它就更纯粹了,但这实际上意味着URI中的a_id被完全忽略,这似乎很可惜.
tl; dr:ParamsWrapper使用request_parameters作为参数源,因此跳过了URI-globbed变量.这是一个Rails错误吗?纯REST倡导者可能会说不,但实用主义暗示是.