Rails - 从一个表单创建多个对象

时间:2014-10-17 16:06:02

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

我有一个表格,填写后会更新一个或多个“治疗适应”对象的属性。我想从这些对象中的每一个中获取三个属性,并在每次填写和保存表单时使用它们创建“任务”对象 - 隐藏字段(:name),:adaptation_details(将成为:content in任务对象)和:researcher / agency_leader /:direct_provider将成为:assigned_to任务。我得到一个未定义的方法`merge':content:Symbol error:

形式:

= form_tag update_individual_treatment_adaptations_path, :method => "put", :id => "myAdaptationsForm" do |f|    
- for treatment_adaptations in @current_agency.treatment_adaptations.order('created_at ASC')
    =fields_for "treatment_adaptations[]", treatment_adaptations do |f|
        - if treatment_adaptations.not_doing_adaptation != 'true'
            %tr
                %td
                    = f.hidden_field :name, :value => treatment_adaptations.name
                %td
                    = f.text_area :adaptation_details, :content, :rows => 10
                %td
                    = f.check_box :treatment_developer, :assigned_to
                    Treatment Developer
                    %br     
                    = f.check_box :researcher, :assigned_to
                    Researcher
                    %br     
                    = f.check_box :agency_leader, :assigned_to
                    Agency Leader
                    %br     
                    = f.check_box :supervisor, :assigned_to
                    Supervisor
                    %br 
                    = f.check_box :direct_provider, :assigned_to
                    Direct Supervisor
                    %br 
                    = f.check_box :implementation_team, :assigned_to
                    Direct Supervisor
                    %br 
                    %br
                    Other:
                    %br
                    = f.text_area :other_implementer, :assigned_to
= submit_tag 'Save', class: 'btn btn-primary'

控制器:

def update_individual
    @task = @current_agency.tasks.build(treatment_adaptations_params[:task])

    @treatment_adaptations = TreatmentAdaptation.update(params[:treatment_adaptations].except(:task).keys, params[:treatment_adaptations].except(:task).values).reject { |p| p.errors.empty? }
    if @treatment_adaptations.empty?
        set_flash "adaptations updated"
        redirect_to :back
    else
        set_flash "adaptations could not be updated", :error
        redirect_to :back
    end
end


private

    def treatment_adaptations_params
        params.require( :treatment_adaptation ).permit( :org_level, :provider_factors, :client_chars, 
        :system_level, :other_reasons, :concerns, :other_concerns, :proceed_with_plan, :seek_further_consultation,
        :address_concern, :address_concern_text, :adaptation_details, :treatment_developer, :researcher, :agency_leader,
        :supervisor, :direct_provider, :implementation_team, :other_implementer, task: [:name, :content, :assigned_to])
    end

1 个答案:

答案 0 :(得分:0)

我的解决方案是在treatment_adaptation模型上使用after_save回调来创建将拉过必要的treatment_adaptation属性的任务。