Ruby Mongoid的嵌套属性有一个关系
我正在尝试创建一个联系人,该联系人与客户有着密切的关系。我使用嵌套属性来执行此操作。我正在新视图/控制器中正确构建联系人。当我去保存联系人时,它告诉我联系人必须存在。因此,出于某种原因,它没有创建联系人 错误: 参数: 型号: 控制器: 视图:Ruby Mongoid的嵌套属性有一个关系,ruby,ruby-on-rails-3,mongoid,Ruby,Ruby On Rails 3,Mongoid,我正在尝试创建一个联系人,该联系人与客户有着密切的关系。我使用嵌套属性来执行此操作。我正在新视图/控制器中正确构建联系人。当我去保存联系人时,它告诉我联系人必须存在。因此,出于某种原因,它没有创建联系人 错误: 参数: 型号: 控制器: 视图: 我认为您需要删除您的客户模型中的联系人id引用。该协会在您的联系人中输入外键。不是你的客户。因此,当您从客户机创建联系人时,只有客户机id在您的联系人中定义,而没有客户机的联系人id 因此,删除行: validates :contact_id, pres
我认为您需要删除您的客户模型中的联系人id引用。该协会在您的联系人中输入外键。不是你的客户。因此,当您从客户机创建联系人时,只有客户机id在您的联系人中定义,而没有客户机的联系人id 因此,删除行:
validates :contact_id, presence: true
# Fields
field :contact_id
在您的客户机模型中
{
"utf8"=>"✓",
"authenticity_token"=>"ep6es356WY5dja7D7C5Kj8Qc0Yvuh3IN2Z1iGG08J7c=",
"client"=>{
"contact_attributes"=>{
"first_name"=>"Scott",
"last_name"=>"Baute"
},
"commit"=>"Create Client"
}
class Client
include Mongoid::Document
include Mongoid::Timestamps
attr_accessible :role, :contact_id, :contact_attributes
# Relationships
belongs_to :firm, validate: true
has_one :contact, validate: true, autosave: true
# Matters is custom relationship
has_many :client_roles
# Nested Attr
accepts_nested_attributes_for :contact
validates :contact_id, presence: true
# Fields
field :contact_id
field :test
end
class Contact
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
#Relationships
belongs_to :client
field :first_name
field :last_name
end
# GET /clients/new
# GET /clients/new.json
def new
@client = current_firm.clients.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @client }
end
end
# POST /clients
# POST /clients.json
def create
@client = current_firm.clients.new(params[:client])
respond_to do |format|
if @client.save!
format.html { redirect_to client_path(@client.contact.id), notice: 'Client was successfully created.' }
format.json { render json: @client, status: :created, location: @client }
else
format.html { render action: "new" }
format.json { render json: @client.errors, status: :unprocessable_entity }
end
end
end
- @client.build_contact unless @client_contact
= semantic_form_for @client, html: { class: "form-horizontal"} do |f|
.control-group
= render "contact_fields", f: builder
.form-actions
= f.submit class: "btn btn-primary"
validates :contact_id, presence: true
# Fields
field :contact_id