gpt4 book ai didi

ruby-on-rails - 使用accepts_nested_attributes_for时不能为空(Rails 4)

转载 作者:行者123 更新时间:2023-12-02 11:37:37 24 4
gpt4 key购买 nike

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

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

这是我的目标和联系模型,以及联系 Controller :

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

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

谢谢!

最佳答案

我找到了另一个解决方案。您可以在 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/

关于ruby-on-rails - 使用accepts_nested_attributes_for时<Child's Parent>不能为空(Rails 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23463912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com