Search code examples
ruby-on-railsnested-forms

nested form has_one not setting form attributes rails 4


I have three models namely "Employee::TaxSettingBatch", "Employee::HousingLoanInterest", "Employee::InvestmentBankDetail". these models are related to each other as follows

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

The problem is that when I am trying to save the data I am getting below error.

Problem: Attempted to set a value for 'employee_housing_loan_interest' which is not allowed on the model Employee::TaxSettingBatch.

My parameters are:

{"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"}

Parameters for "employee_housing_loan_interest" are not getting send as "employee_housing_loan_interest_attributes" that is why I am getting this issue. Help me!

My form :

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

Solution

  • You do not provide field name in your form, so rails takes it from object class name, try:

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