如何为嵌套属性允许强参数? [英] How to permit strong params for nested attributes?

查看:67
本文介绍了如何为嵌套属性允许强参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试接受表单中的嵌套属性时,日志中出现错误Unpermitted parameters: latitude, longitude, address.确切的参数如下:

I'm getting a Unpermitted parameters: latitude, longitude, address error in the log when I try to accept nested attributes from a form. The exact params look like:

{  
  "widget"=> {
    "owner"=>"100", 
    "name"=>"Widget Co", 
    "locations_attributes" => {
      "0"=> {
        "latitude"=>"51.4794259", 
        "longitude"=>"-0.1026201", 
        "address"=>"123 Fake Street"
      }
    }
  },
  "commit"=>"Create Supplier", 
  "action"=>"create", 
  "controller"=>"widgets"
}

小部件has_many的位置,以及belongs_to位置的小部件.我在widgets_controller中设置了参数,我认为该参数允许将所有内容都设为"0",但似乎没有?

A widget has_many locations, and a location belongs_to a widget. The params are set in the widgets_controller which I thought would permit everything under "0", but doesn't seem to?

def widget_params
  params.require(:widget).permit(:owner, :name, locations_attributes: [{"0" => []}])
end

有没有一种可行的/更好的方法来接受这些参数?

Is there a working / better way to accept these params?

谢谢

推荐答案

尝试一下

def widget_params
  params.require(:widget).permit(:owner, :name, locations_attributes: [:id, :latitude, :longitude, :address])
end

这篇关于如何为嵌套属性允许强参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆