带有茧宝石的铁轨嵌套形式,拒绝关联



配方具有许多成分和方向,每个配方属于食谱。我的views/new.html.haml呈现一个_form部分,该部分呈现嵌套的部分。它在提交时拒绝说所需的嵌套元素不存在。我不确定我在做什么错或缺少。我已经反复介绍了代码和茧文档。任何帮助将不胜感激。

型号/食谱.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

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

视图/食谱/_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'

视图/食谱/_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防止此食谱保存

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

这解决了我的问题。创建新食谱时,Cocoon Gem属性不保存

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

相关内容

  • 没有找到相关文章

最新更新