2014-02-18 3 views
0

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

I n 'accepts_nested_attributes_for'가 제대로 작동하도록하려면 : deliverable_dates_attributes가 전달 항목에 대한 attr_accessible에 추가되어야합니다.

+0

Rails 3에서만 –