I have two models I am working with Events and Reports. Reports and embedded in Events. I am having trouble creating a new report for a particular event.
I think my Report Controller new action needs to look something like this:
@event = Event.find(params[:eventid])
@report = @event.report.build
In my Event model I have the following set:
embeds_one :report
accepts_nested_attributes_for :report
When I try to save I receive the following error:
Mongoid::Errors::NoParent
Here is my Report Model
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?
}
And here is my Event Model
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?
}
Thanks in advance.
Make sure to have the
event_idset on your new report when you’re creating it.You can accomplish this by using
@report = @event.report.build(params[:report])as you hinted, or by ensuring that ‘event_id’ is included in the params hash.