使用accepts_nested_attributes_for(Rails 4)</child's>时,<child's parent =“”>不能为空

时间:2014-05-05 02:28:11

标签: ruby-on-rails ruby-on-rails-4 nested-attributes

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

1 error prohibited this contact from being saved:
Goals contact can't be blank

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

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

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

谢谢!

2 个答案:

答案 0 :(得分:30)

我找到了另一个解决方案。您可以使用inverse_ofhas_many中的belongs_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_id
end

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

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

答案 1 :(得分:17)

线索出现错误:

Goals contact can't be blank

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

validates_presence_of :title, :due_date, :contact_id

尝试删除contact_id以测试它是否接受。如果确实接受了,则表示您没有通过参数将contact_id传递给模型

我们使用inherited resource s,它会自动附加parent_id(如果您使用belongs_to方法)。如果你通过accepts_nested_attributes_for传递参数,我建议只删除contact_id验证,因为我认为它会作为流程的一部分添加