1

我在“timesheet”和“timesheetlines”之间有“accepts_nested_attributes_for”关系。这在编辑(编辑/更新)时间表时效果很好,但是在添加新时间表(新建/创建)时,它会返回以下错误:

tms timesheetlines tms timesheet can't be empty

似乎它不知道新时间表属于哪个时间表。这是时间表中的关系:

has_many :tms_timesheetlines, :dependent => :destroy, :order=>"daynr ASC"
accepts_nested_attributes_for :tms_timesheetlines, :reject_if => lambda { |a| a[:daynr].blank? }, :allow_destroy => true

在“新”动作中,时间表线是建立的:

@timesheet = TmsTimesheet.new
month_lines = Time.days_in_month(@current_period.period_nr).to_i
month_lines.times { @timesheet.tms_timesheetlines.build }

任何想法为什么它在编辑时没有任何问题,但在创建时却没有?谢谢!

更新:

当我在每个时间表行中添加此隐藏字段时,当它是新时间表时,保存新的和编辑的时间表都有效:

<%= tl.hidden_field :tms_timesheet_id, :value => timesheet %>

当它是一个编辑时:

<%= tl.hidden_field :tms_timesheet_id, :value => timesheet.id %>

为什么使两者都起作用的区别?

4

1 回答 1

0

检查您的验证:tms_timesheetlines。我的猜测是它正在验证它的存在timesheet和失败。

如果是这种情况,那么它在创建时失败但传递更新的原因是因为使用嵌套表单,在验证子项(时间表)期间将不知道(存在)父项的 id,但在您更新时会存在记录。

于 2012-05-28T20:01:40.773 回答