Rails 4允许散列中的任何键

时间:2015-06-17 14:10:01

标签: ruby-on-rails ruby ruby-on-rails-4 strong-parameters

我通过了这样的参数

{
  "utf8" => true,
  "supply" => {
    "items" => { 111 => 112, 89 => 10},
    "another_params" => "something"
  }
}

我的supply_params是:

params.fetch(:supply, {}).permit(:another_params, items: {})

但我得到unpermitted parameters 111 and 89。如何让items允许各种密钥?

2 个答案:

答案 0 :(得分:1)

github中的

This thread提供了一个解决方案:

def supply_params
  params.require(:supply).permit(:another_params).tap do |whitelisted|
    whitelisted[:items] = params[:supply][:items] if params[:supply][:items]
  end
end

这个想法是明确允许所需的任何已知属性,然后使用嵌套属性。

答案 1 :(得分:0)

根据@steve klein link to github issue,这被认为是一个很好的解决方案:

params.permit(:test).tap do |whitelisted|
  whitelisted[:more] = params[:more]
end
相关问题