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_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
end
Run Code Online (Sandbox Code Playgroud)
我不确切知道它为什么会起作用,但我认为这是因为它允许contact_id
在目标验证之前正确设置.
我找到了解决这篇博文的解决方案:http://homeonrails.com/2012/10/validating-nested-associations-in-rails/
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
验证,因为我认为它将作为过程的一部分添加
归档时间: |
|
查看次数: |
3002 次 |
最近记录: |