使用accepts_nested_attributes_for时,<Child's Parent>不能为空(Rails 4)

gre*_*cki 17 ruby-on-rails nested-attributes ruby-on-rails-4

我有两个模型:我有一个嵌套的表单,允许用户输入@contact和@goal的属性.但是,当我去保存表单输入时,我收到以下错误:

1 error prohibited this contact from being saved:
Goals contact can't be blank
Run Code Online (Sandbox Code Playgroud)

以下是我的目标和联系模式,以及联系人控制器:

class Contact < ActiveRecord::Base

  belongs_to :user
  has_many :goals
  accepts_nested_attributes_for :goals, allow_destroy: true
  validates_presence_of :user_id,:name,:title,:email
end

class Goal < ActiveRecord::Base
  belongs_to :contact
  validates_presence_of :title, :due_date, :contact_id
end

class ContactsController < ApplicationController
  before_action :set_contact, only: [:show, :edit, :update, :destroy]

  # GET /contacts
  # GET /contacts.json
  def index
    @contacts = current_user.contacts
  end

  # GET /contacts/1
  # GET /contacts/1.json
  def show
  end

  # GET /contacts/new
  def new
    @contact = Contact.new
    1.times { @contact.goals.build }
  end

  # GET /contacts/1/edit
  def edit
  end

  # POST /contacts
  # POST /contacts.json
  def create
    @contact = Contact.new(contact_params.merge(user_id: current_user.id))
    respond_to do |format|
      if @contact.save
        format.html { redirect_to @contact, notice: 'Contact was successfully created.' }
        format.json { render :show, status: :created, location: @contact }
      else
        format.html { render :new }
        format.json { render json: @contact.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /contacts/1
  # PATCH/PUT /contacts/1.json
  def update
    respond_to do |format|
      if @contact.update(contact_params)
        format.html { redirect_to @contact, notice: 'Contact was successfully updated.' }
        format.json { render :show, status: :ok, location: @contact }
      else
        format.html { render :edit }
        format.json { render json: @contact.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /contacts/1
  # DELETE /contacts/1.json
  def destroy
    @contact.destroy
    respond_to do |format|
      format.html { redirect_to contacts_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_contact
      @contact = Contact.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def contact_params
      params.require(:contact).permit(:name, :title, :company, :email, :notes, goals_attributes: [:title, :due_date, :notes, :contact_id])
    end
end
Run Code Online (Sandbox Code Playgroud)

我确保将goals_attributes添加到联系参数(在Contacts Controller中).对问题可能是什么的任何想法?这是项目的链接:https://github.com/nowgeez/radiusapp

谢谢!

Gab*_*ira 31

我发现了另一个解决方 您可以使用inverse_of内部选项has_manybelongs_to:

class Contact < ActiveRecord::Base
  has_many :goals, inverse_of: :contact
end

class Goal < ActiveRecord::Base
  belongs_to :contact, inverse_of: :goals
  validates_presence_of :title, :due_date, :contact
end
Run Code Online (Sandbox Code Playgroud)

我不确切知道它为什么会起作用,但我认为这是因为它允许contact_id在目标验证之前正确设置.

我找到了解决这篇博文的解决方案:http://homeonrails.com/2012/10/validating-nested-associations-in-rails/

  • `inverse_of`是推荐的解决方案,因为它会分配关联的对象,即使这些对象尚未保留在数据库中,这是嵌套形式中发生的确切事情. (6认同)
  • 这个应该是公认的答案! (4认同)
  • 一个很好的解决方案.你犯了一个错误,它应该是validates_presence_of:联系NOT contact_id. (3认同)

Ric*_*eck 17

线索出现错误:

Goals contact can't be blank
Run Code Online (Sandbox Code Playgroud)

在您的Goal模型中,您有以下验证:

validates_presence_of :title, :due_date, :contact_id
Run Code Online (Sandbox Code Playgroud)

尝试删除contact_id以测试它是否接受.如果确实接受了,那就意味着你没有将contact_id你的参数传递给模型

我们使用继承资源 s,它会自动附加parent_id(如果使用它们的belongs_to方法).如果您通过了参数accepts_nested_attributes_for,我建议只删除contact_id验证,因为我认为它将作为过程的一部分添加

  • 删除验证是一种创可贴,并且可以在其他一些代码中创建没有关联联系人的目标,这是不希望的。使用 `inverse_of` 回答这个问题的另一个答案是更好的解决方案。 (2认同)
  • 另外,如果你使用`inverse_of`方法,它应该是`validates_presence_of:contact`(*not*`:contact_id`.) (2认同)