繁体   English   中英

带Cocoon宝石的Rails嵌套表格,拒绝关联

[英]Rails nested forms w/ Cocoon gem, rejecting association

食谱有很多成分和说明,每个成分和说明都属于一个食谱。 我的views/new.html.haml渲染了一个_form局部,它渲染了嵌套的局部。 提交后它拒绝说所需的嵌套元素不存在。 我不确定自己在做什么错或遗漏。 我已经反复检查了代码和茧文档。 任何帮助将不胜感激。

模型/recipe.rb

has_many :ingredients
has_many :directions

accepts_nested_attributes_for :ingredients, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :directions, reject_if: :all_blank, allow_destroy: true

_form.html.haml部分中使用simple_form gem

        %h3 Ingredients
        #ingredients
          = f.simple_fields_for :ingredients do |ingredient|
            = render 'ingredient_fields', f: ingredient
          .links
            = link_to_add_association 'Add Ingredient', f, :ingredients

        %h3 Directions
        #directions
          = f.simple_fields_for :directions do |direction|
            = render 'direction_fields', f: direction
          .links
            = link_to_add_association 'Add Step', f, :directions

  = f.button :submit

这链接到每个嵌套元素,成分和方向的局部...

views / recipes / _direction_fields.html.haml

.nested-fields
  = f.input :step, input_html: { class: 'form-input form-control' }
  = link_to_remove_association "Remove Step", f, class: 'btn btn-default form-button'

views / recipes / _ingredient_fields.html.haml

.nested-fields
  = f.input :name, input_html: { class: "form-input form-control" }
  = link_to_remove_association "Remove", f, class: "form-button btn btn-default"

当我尝试提交包含指导和成分的新食谱时,我会得到回滚和快速通知阅读...

2阻止保存此配方

成分配方必须存在方向配方必须存在

这解决了我的问题。 创建新配方时不保存茧宝石属性

Rails 5现在要求您在模型关联中声明inverse_of

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM