[英]how to permit one of the parameters to be arbitrary hash using strong_parameters?
I have a model with one of the fields being serialise :config, JSON
. 我有一个模型,其中一个字段是
serialise :config, JSON
。
it should permit any hash as a value. 它应该允许任何哈希值。 But I can't quite see a clean way to do it with
strong_parameters
. 但是我看不出使用
strong_parameters
做到这一点的干净方法。
my current solution is: 我当前的解决方案是:
def resource_params
p = params.require(:model)
config = dp.slice(:config).permit!
p.delete(:config)
[p.permit(:foo, :bar, ...).merge(config)]
end
From strong_params gem page 从strong_params宝石页面
To whitelist an entire hash of parameters, the permit!
要将参数的整个哈希表列入白名单,请允许! method can be used
可以使用的方法
params.require(:log_entry).permit!
params.require(:log_entry).permit!
And also 并且
If you want to make sure that multiple keys are present in a params hash, you can call the method twice:
如果要确保在params哈希中存在多个键,则可以两次调用该方法:
params.require(:token)
params.require(:token)
params.require(:post).permit(:title)params.require(:post).permit(:title)
but I'm no expert on that matter. 但我在这方面不是专家。
You have to use whitelisting: 您必须使用白名单:
def resource_params
params.require(:model).permit(:foo, :bar, ...).tap do |whitelisted|
whitelisted[:config] = params[:model][:config]
end
end
You can find it here: http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters search for "4.5.4 Outside the Scope of Strong Parameters" 您可以在此处找到它: http : //guides.rubyonrails.org/action_controller_overview.html#strong-parameters搜索“ 4.5.4在强参数范围之外”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.