[英]Rails 3.2.9 accepts_nested_attributes_for confusion
I have a model Event and a model Serie. 我有一个Event模型和一个Serie模型。 They are defined like this: 它们的定义如下:
in event.rb belongs_to :serie accepts_nested_attributes_for :serie attr_accessible :serie in serie.rb has_many :events
Event table has the serie_id key. 事件表具有serie_id键。
In the event form I have pretty much the following: 在活动表格中,我几乎有以下内容:
form_for @event do |f| ... ... f.fields_for @event.serie do |serie_f| .... end end
The request to the controller is coming like this: 对控制器的请求如下所示:
... "event"=>{...., "serie"=>{"..."=>"19/12/2012", ....}, ...}...
In the create action of events_controller I have: 在events_controller的创建动作中,我具有:
def create @event = current_user.events.new(params[:event]) ....
In this line I get the following error: 在此行中,出现以下错误:
ActiveRecord::AssociationTypeMismatch (Serie(#134112340) expected, got ActiveSupport::HashWithIndifferentAccess(#92663620))
Unsure what the problem is 不确定是什么问题
You should check this question : ActiveRecord::AssociationTypeMismatch when attempting to save nested attributes in Rails . 尝试在Rails中保存嵌套属性时,应检查以下问题: ActiveRecord :: AssociationTypeMismatch 。
I think that you simply have to replace attr_accessible :serie
by attr_accessible :serie_attributes
in your model. 我认为您只需在模型attr_accessible :serie
替换为attr_accessible :serie_attributes
。
And in your view, try f.fields_for :serie do |serie_f|
在您看来,请尝试f.fields_for :serie do |serie_f|
instead of f.fields_for @event.serie do |serie_f|
代替f.fields_for @event.serie do |serie_f|
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.