Ruby on rails 创建时未保存嵌套对象

Ruby on rails 创建时未保存嵌套对象,ruby-on-rails,field,nested-form-for,Ruby On Rails,Field,Nested Form For,我正在尝试制作一个新的可交付成果,其中包含嵌套的可交付成果日期。可交付成果的属性(如标题)保存,但可交付成果日期中的嵌套属性不保存。我错过了什么 非常感谢 class ProgramManager::DeliverablesController < ProgramManager::ApplicationController ... def new @deliverable = @program.deliverables.build @program.groups.eac

我正在尝试制作一个新的可交付成果,其中包含嵌套的可交付成果日期。可交付成果的属性(如标题)保存,但可交付成果日期中的嵌套属性不保存。我错过了什么

非常感谢

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
class ProgramManager::DeliverablesController
-


...
...
-

可交付类
-

class DeliveryDate{其中('(过期日期不为空且过期日期<?)或(过期日期不为空且过期日期<?)',date.today,date.today)}
范围:即将到来,->{其中('(输入日期不为空且输入日期>=?)和(输出日期不为空且输出日期>=?)或(截止日期不为空且截止日期>=?),date.today,date.today,date.today)}

范围:当前可交付成果,->{where(”((按日期>和按日期?和入日期?和入日期=?和出日期)为了使“接受嵌套属性”正常工作:必须将可交付成果的日期属性添加到可供交付成果访问的属性中

<%= 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