ruby-on-rails – has_one关联的嵌套属性

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – has_one关联的嵌套属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要设置属性,在new和edit动作中有一个关联,所以我有这个:

产品型号

has_one :store
accepts_nested_attributes_for :store

形成

= form_tag @product do |f|
  = f.fields_for :store do |store_fields|
    = render 'store_form',:f => store_fields

在控制器中

params.require(:store).permit(:store).permit!

字段显示,但是当我提交表单时,它没有意义,商店关联是空的.问题怎么解决

UPD

params.require(:product).permit(store_attributes: [:store_id,:supplier_id,:margin,:discount]).permit!

日志:

Parameters: {"utf8"=>"✓","authenticity_token"=>"...","product"=>{"name"=>"qwefqwefasdf","description"=>"","permalink"=>"asdf","store_attributes"=>{"margin"=>"123","discount"=>"123"}},"button"=>"","id"=>"asdf"}

解决方法

好的,正确的答案是

更改

= f.fields_for :store do |store_fields|

= f.fields_for :store,@vendor.store do |store_fields|
原文链接:https://www.f2er.com/ruby/271145.html

猜你在找的Ruby相关文章