嵌套的简单表单不保存数据库中的属性

时间:2016-07-19 20:26:34

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

我读了很多关于这个问题的答案,但我还没有找到解决方案......

我有一家公司,其中有很多公司工作场所结束了部分has_many CompanyWorkplace。

公司型号:

class Company < ActiveRecord::Base
    has_many :company_tutors, dependent: :destroy, inverse_of: :company
    has_many :offices, dependent: :destroy
    has_many :contracts
    has_many :company_workplaces

    accepts_nested_attributes_for :company_workplaces, reject_if: :all_blank, allow_destroy: true
    accepts_nested_attributes_for :company_tutors, reject_if: :all_blank, allow_destroy: true
    accepts_nested_attributes_for :offices, reject_if: :all_blank, allow_destroy: true

专业化模型:

 class Specialization < ActiveRecord::Base

      has_many :sections
      has_many :company_workplaces

CompanyWorplace模型:

class CompanyWorkplace < ActiveRecord::Base
  belongs_to :company
  belongs_to :specialization
end

这是公司控制人员

def new
    @company_tutor = CompanyTutor.new
    @office = Office.new
    @company = Company.new
    @uuid = SecureRandom.uuid
    Specialization.find_each do |specialization|

       company_workplace =  CompanyWorkplace.new(specialization_id: specialization.id, uuid: @uuid)
       company_workplace.save!
     end
      @company.company_workplaces.build


    respond_with(@company)
end

def company_params
        params[:company] and params[:company_workplace]
    end

好的,我做了这个表格:

     <% modal ||= false %>
        <% remote = modal ? true : false %>
        <% fmt = modal ? :json : :html %>
        <%= simple_form_for @company, remote: remote,  format: fmt, html: {role: :form, 'data-model' => 'company', class: (modal ? "modal-footer form-horizontal" : "form-horizontal"), id: 'form1'} do |f|  %>
      --- other code -- 
<% if @company.persisted?%>
    <%CompanyWorkplace.where(company_id: @company.id).each do |company_workplace|%>
        <%=f.simple_fields_for company_workplace  do |cw|%>
          <div class="form-group">
              <label class="col-md-3 control-label"><%= Specialization.find(company_workplace.specialization_id)%></label>
              <div class="col-md-4">
                  <%= cw.input_field :posti,  class: 'form-control'%>
              </div>
          </div>
        <% end %>
      <% end %>
    <%else%>
      <%CompanyWorkplace.where(uuid: @uuid).each do |company_workplace|%>

        <%=f.simple_fields_for company_workplace do |cw|%>
          <div class="form-group">
              <label class="col-md-3 control-label"><%= Specialization.find(company_workplace.specialization_id)%></label>
              <div class="col-md-4">
                  <%= cw.input_field :posti,  class: 'form-control'%>
              </div>
          </div>
        <% end %>
      <% end %>
    <% end %>

这是我保存表单时得到的日志:

Processing by CompanysController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"pZIFeROvjLS+QXcxY3iPXBpUfAhePz6ssrA9rzogOGhfOFCW/ZcaILoL/oJNRGnT4F0ng6XWk4g6yHjnNp/RPA==", "company"=>{"nome"=>"Azienda1", "codice_ATECO"=>"Ca231", "settore"=>"informatica", "partita_IVA"=>"", "codice_fiscale"=>"", "numero_dipendenti_tempo_indeterminato"=>"", "CCNL_applicato"=>"", "rappresentante_legale"=>"", "data_nascita_rappresentante"=>"", "luogo_nascita_rappresentante"=>"", "telefono"=>"", "cell"=>"", "email"=>"", "rappresentante_asl_nome"=>"", "rappresentante_asl_cognome"=>"", "rappresentante_asl_contatto"=>"", "company_workplace"=>{"posti"=>"2"}, "note"=>""}}
   (0.6ms)  BEGIN
  SQL (0.9ms)  INSERT INTO "companys" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"  [["created_at", "2016-07-19 15:41:43.992089"], ["updated_at", "2016-07-19 15:41:43.992089"]]
   (0.8ms)  COMMIT
Redirected to http://localhost:3000/companys/16
Completed 302 Found in 8ms (ActiveRecord: 2.2ms)


Started GET "/Companys/16" for 127.0.0.1 at 2016-07-19 17:41:44 +0200
Processing by CompanysController#show as HTML
  Parameters: {"id"=>"16"}
  Company Load (0.7ms)  SELECT  "companys".* FROM "companys" WHERE "companys"."id" = $1 LIMIT 1  [["id", 16]]
  Rendered companys/show.html.erb within layouts/application (8.8ms)
Completed 500 Internal Server Error in 15ms (ActiveRecord: 0.7ms)

我不明白我错在哪里

0 个答案:

没有答案