2016-05-17 8 views
5

深くネストされたフォームを保存しようとしています。アプリケーション、応募者、住所の3種類のモデルがあります。アドレスは、アプリケーションと応募者の両方が持つことができるため、多態的です。これらは私が送信してるの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: trueAddressbelongs_toに追加した後に正常に機能しました。これは新しいRails 5のコンベンションです。私はそれを私の共同仲間に置くことを覚えましたが、そこにはありません。これが誰かを助けることを願って!

+0

ありがとうございます!これは完全に解決策を見つけるために永遠に検索した後に役立ちます。 – Charles

+0

私のためにこの作品。しかし、私はまだこのエラーが私の他のフォームで以前に発生していない理由が不思議です。 –

関連する問題