【问题标题】:<Child's Parent> can't be blank while using accepts_nested_attributes_for (Rails 4)<Child's Parent> 在使用 accept_nested_attributes_for 时不能为空(Rails 4)
【发布时间】:2014-05-05 02:28:10
【问题描述】:

我有两个模型:我有一个嵌套表单,它允许用户输入@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

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

谢谢!

【问题讨论】:

  • 只是好奇。试试这个params.require(:contact).permit(:name, :title, :company, :email, :notes, :goals_attributes =&gt; [:title, :due_date, :notes, :contact_id])
  • 很多人认为我对这个问题的回答是最合适的。如果您同意,您可以选择它作为接受的答案吗?

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


【解决方案1】:

我找到了另一个解决方案。您可以在has_manybelongs_to 中使用inverse_of 选项:

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

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

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

【讨论】:

  • inverse_of 是推荐的解决方案,因为即使这些对象尚未保存在数据库中,它也会分配关联的对象,这正是嵌套形式发生的事情。
  • 一个非常好的解决方案。你犯了一个错误,它应该是 validates_presence_of :contact NOT contact_id。
  • 我刚去更新rails guides,已经在edge guides中了。
【解决方案2】:

线索在错误中:

Goals contact can't be blank

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

validates_presence_of :title, :due_date, :contact_id

尝试删除contact_id 以测试它是否会接受。如果它接受,则意味着您没有通过参数将contact_id 传递给模型

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

【讨论】:

  • 删除验证是一种创可贴,并且可以在不使用其他代码的关联联系人的情况下创建目标,这是不希望的。使用inverse_of 对这个问题的另一个答案是更好的解决方案。
  • 另外,如果你使用inverse_of方法,应该是validates_presence_of :contact不是 :contact_id.)
  • 正如@ghoppe 上面所说,使用inverse_of 是更正确的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-07
  • 2021-12-29
相关资源
最近更新 更多