1

我有两个模型正在处理事件和报告。报告并嵌入事件中。我在为特定事件创建新报告时遇到问题。

我认为我的报告控制器新操作需要看起来像这样:

@event = Event.find(params[:eventid])
@report = @event.report.build

在我的事件模型中,我有以下集合:

embeds_one :report
accepts_nested_attributes_for :report

当我尝试保存时,我收到以下错误:

Mongoid::Errors::NoParent

这是我的报告模型

class Report
include Mongoid::Document
include Mongoid::Timestamps
field :test, type: String
embedded_in :event, :inverse_of => :report
embeds_many :report_details
accepts_nested_attributes_for :report_details,
  :allow_destroy => true, 
    :reject_if => proc { |attributes| 
      attributes['name'].blank? && attributes['_destroy'].blank? 
    }

这是我的事件模型

class Event
include Mongoid::Document
include Mongoid::Timestamps
embeds_one :report
accepts_nested_attributes_for :report,
:allow_destroy => true, 
  :reject_if => proc { |attributes| 
    attributes['name'].blank? && attributes['_destroy'].blank? 
  }

提前致谢。

4

1 回答 1

4

event_id创建新报告时,请确保在新报告中包含该集。

您可以@report = @event.report.build(params[:report])按照您的提示使用,或确保“event_id”包含在参数哈希中来完成此操作。

于 2012-09-28T20:32:22.497 回答