Ruby on rails <;儿童';s母公司>;can';在为(Rails 4)使用accepts\u nested\u attributes\u时不能为空
我有两个模型:我有一个嵌套表单,允许用户输入@contact和@goal的属性。但是,当我去保存表单输入时,会出现以下错误:Ruby on rails <;儿童';s母公司>;can';在为(Rails 4)使用accepts\u nested\u attributes\u时不能为空,ruby-on-rails,ruby-on-rails-4,nested-attributes,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 accep
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
class联系人
我确保将goals_属性添加到联系人参数(在联系人控制器中)。你对这个问题有什么想法吗?以下是该项目的链接:
谢谢 线索在错误中:
Goals contact can't be blank
在您的
目标
模型中,您有以下验证:
validates_presence_of :title, :due_date, :contact_id
尝试删除联系\u id
以测试它是否接受。如果接受,则表示您没有通过参数将联系人id
传递给模型
我们使用s,它会自动附加父id(如果使用它们的方法)。如果要通过
接受
的嵌套属性\u传递参数,我建议只删除联系人id
验证,因为我认为它将作为过程的一部分添加我找到了另一个解决方案。您可以使用中的inverse\u 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
class联系人
我不知道它到底为什么有效,但我相信这是因为它允许在目标验证之前正确设置联系人id
我在阅读这篇博文时发现了解决方案:只是好奇。像这样尝试params.require(:contact)。permit(:name,:title,:company,:email,:notes,:goals\u attributes=>[:title,:due\u date,:notes,:contact\u id])
。许多人认为我对这个问题的回答是最恰当的。如果您同意,您可以选择它作为一个可接受的答案吗?
的逆\u是推荐的解决方案,因为它将分配关联的对象,即使这些对象尚未在数据库中持久化,这正是以嵌套形式发生的事情。这是一个非常好的解决方案。虽然你犯了一个错误,但它应该是有效的:联系不接触Twitter ID。我只是去更新Rails指南,它已经在边缘向导中。移除验证是一个创可贴,并且可以在没有其他代码的情况下创建一个没有关联的联系的目标,这是不希望的。使用inverse\u of
回答这个问题的另一个答案是更好的解决方案。此外,如果使用inverse\u of
方法,它应该是验证:contact
的存在(而不是:contact\u id
),正如@ghoppe上面所说的,使用inverse\u of
是更正确的答案。