accepted_nested_attributes_for编辑/更新,而不是新/创建
问题描述:
我有“时间表”和“时间表”之间的“accep_nested_attributes_for”关系。accepted_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
并在“新”行动timesheetlines是构建:
@timesheet = TmsTimesheet.new
month_lines = Time.days_in_month(@current_period.period_nr).to_i
month_lines.times { @timesheet.tms_timesheetlines.build }
任何想法,为什么它在编辑时工作没有任何问题,但没有创造什么时候?谢谢!
更新:
两个保存新的和编辑的时间表作品,当我加入这个隐藏字段时,它是一个新的时间表每个timesheetline:
<%= tl.hidden_field :tms_timesheet_id, :value => timesheet %>
这当是一个编辑:
为什么这两者的区别工作?
答
检查您的验证:tms_timesheetlines
。我的猜测是,它正在验证timesheet
的存在并在此失败。
如果是这种情况,那么创建失败而传递更新的原因是因为对于嵌套窗体,在验证子窗体(时间表格)时,父窗口的ID不会被识别(存在),但是会在你来更新记录。
我已经用一些有效的方法更新了我的问题,但在我看来仍然很奇怪。 – John 2012-04-12 18:31:37