2016-05-17 58 views
5

我試圖保存一個深度嵌套的表單。有三種不同的模式:應用程序,申請人和地址。地址是多態的,因爲申請和申請人都可以擁有。這些都是我送PARAMS:多態關聯和嵌套屬性:: error =>:blank

{"application"=>{ 
    "loan_type"=>"purchase", 
    "loan_amount"=>2500000, 
    "borrower_attributes"=>{ 
     "first_name"=>"Test", 
     "middle_name"=>"Test", 
     "last_name"=>"Test", 
     "birthdate"=>"01/01/1970", 
     "phone"=>"1231231234", 
     "email"=>"[email protected]", 
     "marital_status"=>"married", 
     "address_attributes"=>{ 
      "street_address"=>"xxxx Mission Street", 
      "city"=>"San Francisco", 
      "state"=>"CA", 
      "zip"=>xxxxx 
     } 
    } 
}} 

當這在我的請求被髮送,我得到如下回應:

<Address id: nil, street_address: "1045 Mission Street", city: "San Francisco", state: "CA", zip: 94103, addressable_type: "Applicant", addressable_id: nil, created_at: nil, updated_at: nil> 

而且出現以下錯誤信息:

@messages={:"borrower.address.addressable"=>["must exist"]}, @details={"borrower.address.addressable"=>[{:error=>:blank}]} 

爲什麼我無法設置嵌套屬性?我需要做些什麼才能完成這項工作?

這裏是我的相關文件:

application.rb中

class Application < ApplicationRecord 
    before_save :assign_addresses! 
    belongs_to :borrower, class_name: 'Applicant' 
    belongs_to :coborrower, class_name: 'Applicant', optional: true 
    has_one :address, as: :addressable 

    accepts_nested_attributes_for :borrower 
    accepts_nested_attributes_for :coborrower 
    accepts_nested_attributes_for :address 
end 

Applicant.rb

class Applicant < ApplicationRecord 
    has_many :applications 
    has_one :address, as: :addressable 
    validates_presence_of :first_name, :last_name, :phone, :email, :birthdate 

    accepts_nested_attributes_for :address 
end 

Address.rb

class Address < ApplicationRecord 
    belongs_to :addressable, polymorphic: true 
    validates_presence_of :street_address, :city, :state, :zip 
end 

Applications_Controller.rb

class Api::ApplicationsController < ApplicationController 

    ... 

    def create 
     @application = Application.new(application_params) 

     if @application.save 
      render json: @application, status: :created 
     else 
      render json: @application.errors, status: :unprocessable_entity 
     end 
    end 

    private 

    def application_params 
     params.require(:application).permit(:loan_type, :loan_amount, borrower_attributes: [:first_name, :middle_name, :last_name, :birthdate, :phone, :email, :ssn, :marital_status, address_attributes: [:street_address, :city, :state, :zip]]) 
    end 
end 

回答

0

只是建立這樣的:

{"application"=>{ 
"loan_type"=>"purchase", 
"loan_amount"=>2500000, 
"applications_borrower_attributes"=>{"0"=>{ 
    "first_name"=>"Test", 
    "middle_name"=>"Test", 
    "last_name"=>"Test", 
    "birthdate"=>"01/01/1970", 
    "phone"=>"1231231234", 
    "email"=>"[email protected]", 
    "marital_status"=>"married"}, 
    "borrowers_address_attributes"=>{"0"=>{ 
     "street_address"=>"xxxx Mission Street", 
     "city"=>"San Francisco", 
     "state"=>"CA", 
     "zip"=>xxxxx 
    } 
    } 
    } 
}} 
+0

沒有運氣。另外,爲什麼包含「0」? –

+0

這就是它在具有嵌套屬性的窗體上生成的方式@LouisCruz – Jason

6

發現的問題。將optional: true添加到Addressbelongs_to之後,情況良好。這是一個新的Rails 5慣例。我記得把它放在我的同舟共濟,但不在那裏。希望這可以幫助別人!

+0

感謝Louis!這完全有助於永久搜索找到解決方案。 – Charles

+0

這個工作適合我。但即時通訊仍然好奇爲什麼這個錯誤不會在我的其他表單上出現。 –