嵌套对象未在create上保存

时间:2014-02-18 15:00:46

标签: ruby-on-rails field nested-form-for

我正在尝试创建一个嵌套DeliverableDates的新Deliverable。 Deliverable的属性(如title)保存但DeliverableDates中的嵌套属性不保存。我错过了什么?

非常感谢

class ProgramManager::DeliverablesController < ProgramManager::ApplicationController
...
  def new
    @deliverable = @program.deliverables.build
    @program.groups.each do |group|
      @deliverable.deliverable_dates.build(group_id: group.id)
    end
    clean_deliverables
    3.times { @deliverable.select_options.build }
  end

  def create
    delete_empty_select_options
    @deliverable = @program.deliverables.new(params[:deliverable])
    clean_deliverables
    if @deliverable.save
      redirect_to program_deliverables_path(@program), success: 'Deliverable created successfully'
    else
    @program.groups.each do |group|
      @deliverable.deliverable_dates.build(group_id: group.id)
    end
      render :new
    end
  end
...
end

-

<%= form_for [@program, deliverable], html: { class: 'form-horizontal' } do |f| %>
  <%= render 'shared/error_messages', object: deliverable %>
...

  <div class="in-out <%= "hidden" if deliverable.is_by_date? %>" id="in-out">
    <%= f.fields_for :deliverable_dates do |d| %>
      <h5><%= Group.find(d.object.group_id).name %></h5>
      <div class="form-group">
        <%= d.label :in_date, 'In Date', class: 'col-md-2 control-label' %>
        <div class="col-md-10">
          <%= d.text_field :in_date, class: 'form-control input-sm datepicker', id: 'deliverable_in_date_new', placeholder: 'In Date' %>
        </div>
      </div>

      <div class="form-group">
        <%= d.label :out_date, 'Out Date', class: 'col-md-2 control-label' %>
        <div class="col-md-10">
          <%= d.text_field :out_date, class: 'form-control input-sm datepicker', id: 'deliverable_out_date_new', placeholder: 'Out Date' %>
        </div>
      </div>
    <% end %>
  </div>

...

  <div class="form-group">
    <div class="col-md-10 col-md-offset-2">
      <%= f.submit 'Save changes', class: 'btn btn-primary' %>
    </div>
  </div>
<% end %>

-

class Deliverable < ActiveRecord::Base
  include Folderable
  include Reportable

  attr_accessible :program_id, :deliverable_type, :is_by_date, :title, :file_cabinet_folder, :select_options_attributes

  attr_accessor :in_data_cell, :out_data_cell, :by_data_cell

  belongs_to :program
  has_many :deliverable_dates, dependent: :destroy
  has_many :select_options, as: :optionable, dependent: :destroy
  has_many :deliverable_data, dependent: :destroy
  has_many :folders, as: :folderable, dependent: :destroy

  delegate :in_date, :out_date, :by_date, to: :deliverable_dates

  accepts_nested_attributes_for :select_options, allow_destroy: true
  accepts_nested_attributes_for :deliverable_dates, allow_destroy: true

  ...
end

-

class DeliverableDate < ActiveRecord::Base
  attr_accessible :group_id, :deliverable_id, :in_date, :out_date, :by_date

  belongs_to :group
  belongs_to :deliverable

  validates :group_id, presence: true
  validates :deliverable_id, presence: true

  scope :past_due, -> { where('(out_date is not null and out_date < ?) or (by_date is not null and by_date < ?)', Date.today, Date.today) }
  scope :upcoming, -> { where('((in_date is not null and in_date >= ?) and (out_date is not null and out_date >= ?)) or (by_date is not null and by_date >= ?)', Date.today, Date.today, Date.today) }
  scope :current_deliverables, -> { where('((by_date > ? and by_date <= ?) or (in_date > ? and in_date <= ?) or (out_date > ? and in_date <= ?) or (in_date >= ? and out_date <= ?))', Date.today, 10.days.from_now, Date.today, 5.days.from_now, Date.today, 5.days.from_now, Date.today, Date.today) }

end

1 个答案:

答案 0 :(得分:1)

为了使'accepts_nested_attributes_for'正常工作,“:deliverrable_dates_attributes”必须添加到ative_accessible for Deliverable