嵌套表单has_one不设置表单属性rails 4

时间:2018-12-18 12:31:46

标签: ruby-on-rails nested-forms

我有三个模型,分别是“ Employee :: TaxSettingBatch”,“ Employee :: HousingLoanInterest”,“ Employee :: InvestmentBankDetail”。这些模型相互关联如下

class Employee::TaxSettingBatch
  include Mongoid::Document
  has_one :housing_loan_interest, class_name: Employee::HousingLoanInterest'
  accepts_nested_attributes_for :housing_loan_interest, allow_destroy: true
end

class Employee::HousingLoanInterest
  include Mongoid::Document
  field :property_type, type: String    
  belongs_to :employee_tax_setting_batch, class_name: 'Employee::TaxSettingBatch'    
  has_many :investment_bank_details, class_name: 'Employee::InvestmentBankDetail'
  accepts_nested_attributes_for :investment_bank_details, allow_destroy: true
end

问题是,当我尝试保存数据时,出现错误以下信息。

问题:   尝试为'employee_housing_loan_interest'设置一个值,该值在Employee :: TaxSettingBatch模型上是不允许的。

我的参数是:

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"Afig1spGvAgNX9bj7VEwRLO6FvTVH+ZOtNDBpEHY+LF4c10yn0KUHAlOn5oB/isfOwQF/VHjIcT7Etm2t0MHfA==",
 "section"=>"housing_loan_info",
 "employee_tax_setting_batch"=>
  {"employee_housing_loan_interest"=>
    {"property_type"=>"Self occupied property",
     "_destroy"=>"false",
     "investment_bank_details_attributes"=>
      {"1545132357997"=>
        {"loan_type"=>"Self",
         "construction_completion_month_and_year"=>"jan 200",
         "lender_bank_name"=>"hdfc",
         "lender_pan_number"=>"bcopp15",
         "loan_interest_details_attributes"=>
          {"0"=>{"component"=>"Pre EMI Amount", "declared_amount"=>"2", "verified_amount"=>"3", "remarks"=>""},
           "1"=>{"component"=>"Interest Repayment amount", "declared_amount"=>"2", "verified_amount"=>"3", "remarks"=>""}}}}}},
 "disallowed_amount"=>"",
 "controller"=>"employee/tax_setting_batches",
 "action"=>"update",
 "company_id"=>"55532e18517569120b7b0000",
 "employee_id"=>"55532e18517569120b7c0000",
 "tax_setting_id"=>"5bfe84d8c142e30aa0000125",
 "id"=>"5c0f9676c142e309980002cb"}

“ employee_housing_loan_interest”的参数未作为“ employee_housing_loan_interest_attributes”发送,这就是为什么我遇到此问题。救救我!

我的表格:

<%= nested_form_for @tax_setting_batch, url: company_employee_tax_setting_tax_setting_batch_path(@company, @employee, @tax_setting, @tax_setting_batch), html: {id: 'form-80c-details', class: 'form-horizontal'}, remote:true,:authenticity_token=> true do |f| %>
    <%= f.fields_for @tax_setting_batch.housing_loan_interest.nil? ? @tax_setting_batch.build_housing_loan_interest : @tax_setting_batch.housing_loan_interest , :wrapper => false do |form| %>

1 个答案:

答案 0 :(得分:0)

您没有在表单中提供字段名称,因此Rails从对象类名称中获取它,请尝试:

<%= nested_form_for @tax_setting_batch, ... do |f| %>
  <% f.object.housing_loan_interest.nil? && f.object.build_housing_loan_interest %>
  <%= f.fields_for :housing_loan_interest, wrapper: false do |form| %>
    ...