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

nested form has_one not setting form attributes rails 4

我有三个模型,即"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| %>

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

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