我似乎无法将新的值添加到我的params散列中。我正在尝试将此actor_id
密钥添加到params,但这不起作用:
params.merge(:actor_id => 2)
当我在合并之前和之后使用logger.debug时,我看不到我的actor_id
键。如何添加到params
?
答案 0 :(得分:3)
试试这个
params.merge!(actor_id:2)
它将修改params本身,因为我们正在使用!
答案 1 :(得分:1)
这是因为ActiveSupport::HashWithIndifferentAccess中的方法合并不会修改接收器,而是返回一个新的哈希,其具有与合并结果无关的访问权限。
由于评论建议使用合并!或使用别名更新。
ActiveController::Parameters继承自ActiveSupport :: HashWithIndifferentAccess
# This method has the same semantics of +update+, except it does not
# modify the receiver but rather returns a new hash with indifferent
# access with the result of the merge.
def merge(hash, &block)
self.dup.update(hash, &block)
end