2016-05-17 12 views
5

Tôi đang cố gắng lưu biểu mẫu lồng nhau sâu. Có ba mô hình khác nhau: Ứng dụng, Người đăng ký và Địa chỉ. Các địa chỉ có tính đa hình, vì cả Ứng dụng lẫn Người nộp đơn đều có thể có chúng. Đây là những params rằng tôi đang gửi:Hiệp hội đa hình và thuộc tính lồng nhau:: 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 
     } 
    } 
}} 

Khi điều này được gửi trong yêu cầu của tôi, tôi nhận được câu trả lời sau đây:

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

Và thông báo lỗi sau:

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

Tại sao tôi không thể đặt thuộc tính lồng nhau? Tôi cần sửa chữa gì để thực hiện công việc này?

Dưới đây là các file có liên quan của tôi:

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 

Trả lời

0

chỉ cần xây dựng như thế này:

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

Không may mắn. Ngoài ra, tại sao bạn bao gồm "0"? –

+0

Đó là cách nó được tạo ra trên các biểu mẫu có thuộc tính lồng nhau @LouisCruz – Jason

6

Tìm thấy vấn đề này. Mọi thứ hoạt động tốt sau khi thêm optional: true vào Addressbelongs_to. Đây là một quy ước Rails 5 mới. Tôi nhớ đặt nó vào người đồng tiền của tôi, nhưng không phải ở đó. Hy vọng điều này sẽ giúp một ai đó!

+0

Cảm ơn Louis! Điều này hoàn toàn giúp sau khi tìm kiếm mãi mãi để tìm ra giải pháp. – Charles

+0

tác phẩm này dành cho tôi. Nhưng im vẫn tò mò tại sao lỗi này không xảy ra trước đó trên biểu mẫu khác của tôi. –

Các vấn đề liên quan