2016-05-17 18 views
5

Çok iç içe geçmiş bir formu kaydetmeye çalışıyorum. Üç farklı model vardır: Uygulamalar, Başvuru Sahipleri ve Adresler. Adresler ve Başvuru Sahiplerinin sahip olabileceği gibi, adresler polimorfiktir. Bunlar yolluyorum params şunlardır:Polimorfik İlişkilendirme ve İç içe Öznitelikleri:: 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 
     } 
    } 
}} 

bu benim isteğinde gönderildiğinde, ben şu yanıtı alıyorum:

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

Ve aşağıdaki hata iletisi:

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

İç içe geçmiş nitelikleri ayarlayamıyorum neden? Bu işi yapmak için ne düzeltmem gerekiyor?

İşte benim alakalı dosyalar şunlardır:

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 

cevap

0

aynen böyle inşa:

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

Şans yok. Ayrıca, neden "0" s eklediniz? –

+0

İç içe geçirilmiş özniteliklere sahip formlarda bu şekilde oluşturuldu @LouisCruz – Jason

6

sorunu buldum. İşler optional: true eklendikten sonra Addressbelongs_to'a iyi çalıştı. Bu yeni bir Rails 5 sözleşmesi. Onu kozbayıcım üzerine koyduğumu hatırladım ama orada değil. Umarım bu birisine yardım eder!

+0

Teşekkürler Louis! Bu, çözümü bulmak için sonsuza kadar arandığında tamamen yardımcı olur. Bu çalışma benim için – Charles

+0

. Ama yine de bu hatanın neden diğer formumda gerçekleşmediğini merak ediyorum. –