Rails 5:无法从参数中检索哈希值 [英] Rails 5: unable to retrieve hash values from parameter
问题描述
我遇到了一个奇怪的问题.
I'm running into a strange issue.
undefined method `values' for #<ActionController::Parameters:0x007fb06f6b2728>
是我得到的错误,当我将一个变量分配给一个 param 散列并尝试获取它的值时.
is the error I get, when I assign a variable to a param hash, and try to get it's values.
attributes = params[:line_item][:line_item_attributes_attributes] || {}
attributes.values
参数看起来像这样一个散列的散列:
the parameter looks like this a hash of hashes:
{"0"=>{"product_attribute_id"=>"4"}, "1"=>{"product_attribute_id"=>"7"}}
现在,当我在控制台中执行此操作并将其分配给变量属性时,它可以完美地工作.所以我很难理解什么在这里不起作用 - 以及如何使它起作用.
now when I do this in console and assign that to a variable attributes it works flawlessly. So I'm struggling to understand what isn't working here - and how to make it work.
推荐答案
看看 这个.很奇怪,因为 ActionController::Parameters
是 Hash 的子类,你可以转换它 直接 使用 to_h
方法对 params 散列进行散列.
take a look to this. Very weird since ActionController::Parameters
is a subclass of Hash, you can convert it directly to a hash using the to_h
method on the params hash.
但是 to_h
仅适用于白名单参数,因此您可以执行以下操作:
However to_h
only will work with whitelisted params, so you can do something like:
permitted = params.require(:line_item).permit(: line_item_attributes_attributes)
attributes = permitted.to_h || {}
attributes.values
但如果您不想加入白名单,那么您只需要使用 to_unsafe_h
方法即可.
But if instead you do not want to whitelist then you just need to use the to_unsafe_h
method.
我对这个问题很好奇,所以我开始研究,现在你澄清你使用的是 Rails 5,这就是这个问题的原因,正如@tillmo 在稳定版本的 Rails 4.x 中所说的那样,ActionController::Parameters
是 Hash 的子类,因此它确实应该响应 values
方法,但是在 Rails 5 ActionController::Parameters
现在返回一个 Object 而不是 Hash
I was very curious about this issue, so I started researching, and now that you clarified that you are using Rails 5, well that's the cause of this issue, as @tillmo said in stable releases of Rails like 4.x, ActionController::Parameters
is a subclass of Hash, so it should indeed respond to the values
method, however in Rails 5 ActionController::Parameters
now returns an Object instead of a Hash
注意:这不会影响访问 params 散列中的键,如 params[:id]
.您可以查看实施此更改的拉取请求.
Note: this doesn’t affect accessing the keys in the params hash like params[:id]
. You can view the Pull Request that implemented this change.
要访问对象中的参数,您可以将 to_h
添加到参数中:
To access the parameters in the object you can add to_h
to the parameters:
params.to_h
如果我们查看 ActionController::Parameters
中的 to_h
方法,我们可以看到它在将参数转换为散列之前检查参数是否被允许.
If we look at the to_h
method in ActionController::Parameters
we can see it checks if the parameters are permitted before converting them to a hash.
# actionpack/lib/action_controller/metal/strong_parameters.rb
def to_h
if permitted?
@parameters.to_h
else
slice(*self.class.always_permitted_parameters).permit!.to_h
end
end
例如:
def do_something_with_params
params.slice(:param_1, :param_2)
end
哪个会返回:
{ :param_1 => "a", :param_2 => "2" }
但现在这将返回一个 ActionController::Parameters
对象.
But now that will return an ActionController::Parameters
object.
对此调用 to_h
会返回一个空的哈希值,因为 param_1 和 param_2 是不允许的.
Calling to_h
on this would return an empty hash because param_1 and param_2 aren’t permitted.
要从ActionController::Parameters
访问参数,您需要先允许参数,然后在对象上调用to_h
To get access to the params from ActionController::Parameters
, you need to first permit the params and then call to_h
on the object
def do_something_with_params
params.permit([:param_1, :param_2]).to_h
end
以上将返回一个包含您刚刚允许的参数的哈希值,但如果您不想允许参数并想跳过该步骤,则还有另一种使用 to_unsafe_hash
方法的方法:
The above would return a hash with the params you just permitted, but if you do not want to permit the params and want to skip that step there is another way using to_unsafe_hash
method:
def do_something_with_params
params.to_unsafe_h.slice(:param_1, :param_2)
end
有一种方法可以始终允许来自 application.rb 的配置中的参数,如果您想始终允许某些参数,您可以设置一个配置选项.注意:这将返回带有字符串键的哈希值,而不是符号键.
There is a way of always permit the params from a configuration from application.rb, if you want to always allow certain parameters you can set a configuration option. Note: this will return the hash with string keys, not symbol keys.
#controller and action are parameters that are always permitter by default, but you need to add it in this config.
config.always_permitted_parameters = %w( controller action param_1 param_2)
现在您可以访问以下参数:
Now you can access the params like:
def do_something_with_params
params.slice("param_1", "param_2").to_h
end
注意现在键是字符串而不是符号.
Note that now the keys are strings and not symbols.
希望这有助于您了解问题的根源.
Hope this helps you to understand the root of your issue.
来源:eileen.codes
这篇关于Rails 5:无法从参数中检索哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!