can't permit custom params with strong parameters

佐手、 提交于 2019-12-11 00:17:22

问题


I want to permit nested custom parameters but am not sure how to access them.

These are my params:

params=> 
  {"utf8"=>"✓",
   "authenticity_token"=>"...",
   "tracking"=>{"installation"=>"4", "code"=>[{"1"=>"one", "2"=>"two"}]},
   "action"=>"create",
   "controller"=>"admin/trackings"}

According to the Strong Parameters documentation I tried some combinations like this:

def tracking_params
  params.require(:tracking).permit(:installation, code: [])
end

but they are not working. What am I missing?


回答1:


def tracking_params
  params.require(:tracking).permit(:installation, {:code => []})
end



回答2:


Have you tried solving this on the model level? I think you just need to add:

accepts_nested_attributes_for :tracking

Not sure what your Rails version is, but the docs for Rails 5.1.2 seem to cover this



来源:https://stackoverflow.com/questions/18055466/cant-permit-custom-params-with-strong-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!