我的rails嵌套模型不能识别_destroy属性。

问题描述 投票:0回答:1

我有一个父模型(cocktail)和一个子模型(dose)。

我有一个嵌套的表单,我想在其中能够创建删除dose对象。

创建部分可以在不使用 _destroy 属性,但当我在强参数中添加了 _destroy 的属性哈希,以便能够删除,我得到了错误的信息 unknown attribute '_destroy' for Dose. 我不知道我错在哪里。

另外,我觉得我的代码太复杂了,应该有一个比我现在做的更简单的方法来创建剂量(add_doses方法有必要吗?如果我能得到一些反馈,我将非常感激。

Cocktail:

class Cocktail < ApplicationRecord
  validates :name, uniqueness: true, presence: true

  has_many :doses, dependent: :destroy
  has_many :ingredients, through: :doses
  accepts_nested_attributes_for :doses, allow_destroy: true
end

剂量:

class Dose < ApplicationRecord
  validates :description, presence: true
  belongs_to :cocktail
  belongs_to :ingredient
  validates :cocktail, uniqueness: { scope: :ingredient }
 end

控制器:

class CocktailsController < ApplicationController
  before_action :set_task, only: %i[show edit update]

  def index
    @cocktails = Cocktail.all
  end

  def show; end

  def new
    @cocktail = Cocktail.new
    @cocktail.doses.build
  end

  def create
    @cocktail = Cocktail.new(cocktail_params)
    @cocktail.save
    add_doses
    redirect_to cocktails_path
  end

  def edit; end

  def update
    @cocktail.update(cocktail_params)
    add_doses
    redirect_to cocktail_path(@cocktail)
  end

  private

  def set_task
    @cocktail = Cocktail.find(params[:id])
  end

  def cocktail_params
    params[:cocktail][:name] = params[:cocktail][:name].downcase.titleize
    params.require(:cocktail).permit(:name)
  end

  def add_doses
    @cocktail.doses.destroy_all
    strong_params = params.require(:cocktail).permit(doses_attributes: [:description, :ingredient_id, :_destroy, :id])
    params[:cocktail][:doses_attributes].each_key do |key|
      @cocktail.doses.create(strong_params[:doses_attributes][key])
    end
  end
end

我的主表单视图。

<%= simple_form_for @cocktail do |f| %>
  <%= f.input :name, required: true %>
  <%= f.nested_fields_for :doses do |dose| %>
    <%= render '/cocktails/partials/doses_fields', f: dose %>
  <% end %>
  <div class="btn-group" role="group" aria-label="Basic example">
    <%= link_to_add_association 'add dose', f, :doses, partial: '/cocktails/partials/doses_fields', class: "btn btn-secondary" %>
    <%= f.button :submit, class: "btn btn-secondary" %>
  </div>
<% end %>

我的部分视图可以添加新的剂量

<div class='nested-fields'>
  <div class="field">
    <%= f.text_field :description %>
  </div>
    <%= f.association :ingredient, collection: Ingredient.all %>
    <%= link_to_remove_association "remove dose", f %>
</div>

如果你也想指出我代码中的所有错误,不要忍气吞声,做你最坏的人;)

ruby-on-rails ruby nested-forms strong-parameters
1个回答
2
投票

首先修复你的 #create#update 方法,以便它们在你重定向之前检查记录是否真的被持久化了。

然后将嵌套的参数添加到白名单中,并将其删除。add_doses 方法,这是一个创造性的但有极大缺陷的尝试,它重复了嵌套属性提供的功能。

class CocktailsController < ApplicationController
  before_action :set_cocktail, only: %i[show edit update]

  def index
    @cocktails = Cocktail.all
  end

  def show; end

  def new
    @cocktail = Cocktail.new
    @cocktail.doses.build
  end

  def create
    @cocktail = Cocktail.new(cocktail_params)
    if @cocktail.save
      redirect_to cocktails_path
    else
      render :new
    end
  end

  def edit; end

  def update
    if @cocktail.update(cocktail_params)
      redirect_to @cocktail
    else
      render :edit
    end
  end

  private

  def set_cocktail
    @cocktail = Cocktail.find(params[:id])
  end

  def cocktail_params
    # this should be done in the model
    params[:cocktail][:name] = params[:cocktail][:name].downcase.titleize
    params.require(:cocktail)
          .permit(
             :name,
             doses_attributes: [
               :id,
               :_destroy,
               :description,
               :ingredient_id
             ]
          )
  end
end

如果你想让用户删除嵌套记录,只需创建一个复选框。

<div class='nested-fields'>
  <div class="field">
    <%= f.text_field :description %>
  </div>
    <%= f.association :ingredient, collection: Ingredient.all %>
    <%= f.input :_destroy, as: :boolean, label: 'Remove' %>
</div>

你还需要修正你的验证。

class Dose < ApplicationRecord
  validates :description, presence: true
  belongs_to :cocktail
  belongs_to :ingredient
  validates :cocktail_id, uniqueness: { scope: :ingredient_id }
end

当创建一个唯一性验证时,你需要将其设置为验证实际列的唯一性,而不是关联。

© www.soinside.com 2019 - 2024. All rights reserved.