每次创建新条目时,Rails 嵌套表单都会创建重复项

罗曼夫

我正在尝试模拟食谱和成分之间的关​​系。我希望用户能够创建包含无限量成分的食谱,以及编辑食谱以添加成分。

现在,当用户尝试编辑食谱时,编辑页面会显示已输入的每种成分的表单字段以及空白表单。如果用户提交表单,所有条目都将被复制。

这是我的 recipe.rb:

class Recipe < ApplicationRecord
  has_many :ingredients, :dependent => :delete_all
  accepts_nested_attributes_for :ingredients, reject_if: :all_blank
  validates :title, presence: true, length: {minimum: 4}
  validates :author, presence: true
end

成分.rb:

class Ingredient < ApplicationRecord
  belongs_to :recipe
end

_form.html.erb 是 edit.html.erb 和 new.html.erb 的一部分:

<%= form_for(@recipe) do |form| %>
  <% if @recipe.errors.any? %>
    <div id = "error_explanation">
      <h2>
        <%= pluralize(@recipe.errors.count, "error") %>
        prohibited this recipe from being saved:
      </h2>
      <ul>
        <%@recipe.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <p>
    <%= form.label :title  %><br>
    <%= form.text_field :title %>
  </p>

  <p>
    <%= form.label :author  %><br>
    <%= form.text_field :author %>
  </p>

  <p>
    <%= form.label :note %><br>
    <%= form.text_area :note %>
  </p>

  <table>
    <tr>
      <th> Name </th>
      <th> Amount </th>
      <th> UOM </th>
    </tr>

    <% @recipe.ingredients.each do |ingredient| %>
      <tr>
        <td><%= ingredient.name %></td>
        <td><%= ingredient.amount  %></td>
        <td><%= ingredient.uom %></td>
      </tr>

      </table>

    <% end %>

  <h2>Add an Ingredient: </h2>
  <%= form.fields_for :ingredients do |new| %>
    <p>
      <%= new.label :name %><br>
      <%= new.text_field :name %>
    </p>
    <p>
      <%= new.label :amount %><br>
      <%= new.number_field :amount %>
    </p>
    <p>
      <%= new.label :uom %><br>
      <%= new.text_field :uom %>
    </p>
  <% end %>

  <p>
    <%= form.submit %>
  </p>

<% end %>

recipes_controller.rb

class RecipesController < ApplicationController
  def index
    @recipes = Recipe.all
  end

  def show
    @recipe = Recipe.find(params[:id])
  end

  def new
    @recipe = Recipe.new
    @recipe.ingredients.build
  end

  def edit
    @recipe = Recipe.find(params[:id])
    @recipe.ingredients.build
  end

  def create
    @recipe = Recipe.new(recipe_params)
    if @recipe.save
      redirect_to @recipe
    else
      render 'new'
    end
  end

  def update
    @recipe = Recipe.find(params[:id])

    if @recipe.update(recipe_params)
      redirect_to @recipe
    else
      render 'edit'
    end
  end

  def destroy
    @recipe = Recipe.find(params[:id])
    @recipe.destroy

    redirect_to @recipe
  end

  private
    def recipe_params
      params.require(:recipe).permit(:title, :author, :note, ingredients_attributes: [:name, :amount, :uom])
    end
end

配料控制器.rb

class IngredientsController < ApplicationController

  def create
    @recipe = Recipe.find(params[:recipe_id])
    @ingredient = @recipe.ingredients.create(ingredient_params)
    redirect_to recipe_path(@recipe)
  end

  private
  def ingredient_params
    params.require(:ingredient).permit(:name, :amount, :uom)
  end
end

我见过与此类似的其他问题,但我无法从其中任何一个问题中找到解决我的问题的方法。

最大限度

问题是您没有将嵌套id属性列入白名单

params.require(:recipe)
      .permit(
         :title, :author, :note, 
         ingredients_attributes: [:id, :name, :amount, :uom]
      )

当您的整个参数白名单在某种程度上太长时,这真的很容易错过。accepts_nested_attributes_for如果每个哈希的属性为新记录,则没有 id将处理每个哈希。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

GitLab每次创建新MR时都会显示旧提交

每次创建新实例时,ArrayList都会被覆盖

输入表单时,如何使动态表创建新行?现在,它创建了一行,但是每次点击都会创建一个新行

每次单击按钮时创建一个新表单

Hibernate saveOrUpdate()在更新时创建新条目

创建新条目时添加媒体字段

每次都会创建新的收藏集吗?

SQLAlchemy动态创建条目时重复条目

Android Realm copyToRealmOrUpdate创建嵌套对象的重复项

引用资产目录并从框架中包含资产目录都会创建重复项吗?

Python 类设置器 - 每次创建新实例时都会重新分配类属性吗?

每次Jersey 2.0时,ApplicationScope类都会创建一个新实例

每次运行makemigrations时,Django模型的enum字段都会创建新的迁移,即使未更改

Django模型ImageField每次上传文件时都会创建一个嵌套文件夹

Rails创建方法创建重复项

Ruby on Rails - 创建新条目时使用回调将单独表中的两个对象相乘

每次调用方法时创建一个新的Set

如何防止每次创建新通知时覆盖通知?

Angular 4 共享组件在每次加载时创建新实例

Druid - 防止在每次 CSV 摄取时创建新分区

每次调用函数时如何动态创建新数组?

Rails-使用嵌套表单创建新记录

为什么每次将新条目提交到表单时都重新加载我的HTML页面?

为什么每次创建新的angular 2项目时都必须运行npm install?

openshift:每次成功构建都会创建新的 pod

使用嵌套表单创建用户时的Rails回滚

保存新实体时,在ManyToOne / OneToMany关系上创建的重复条目

熊猫-基于重复项创建新列

为什么每次发布 UWP 打包项目时 Visual Studio 都会创建一个新证书?