1

イベントとレポートで作業しているモデルが2つあります。レポートとイベントに埋め込まれます。特定のイベントの新しいレポートを作成するのに問題があります。

レポートコントローラーの新しいアクションは、次のようになる必要があると思います。

@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'がparamsハッシュに含まれていることを確認することで実現できます。

于 2012-09-28T20:32:22.497 に答える