【问题标题】:Polymorphic Association and Nested Attributes: :error=>:blank多态关联和嵌套属性::error=>:blank
【发布时间】:2016-05-17 16:57:48
【问题描述】:

我正在尝试保存一个深度嵌套的表单。共有三种不同的模型:应用程序、申请人和地址。地址是多态的,因为应用程序和申请人都可以拥有它们。这些是我要发送的参数:

{"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"=>"test@me.com",
        "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

申请人.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

地址.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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5


    【解决方案1】:

    发现问题。将optional: true 添加到Address belongs_to 后一切正常。这是一个新的 Rails 5 约定。我记得把它放在我的共同借款人身上,但不是在那里。希望这对某人有帮助!

    【讨论】:

    • 谢谢路易斯!在永远寻找解决方案之后,这完全有帮助。
    • 这项工作适合我。但我仍然很好奇为什么这个错误没有在我的其他表单上更早发生。
    【解决方案2】:

    像这样构建:

    {"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"=>"test@me.com",
        "marital_status"=>"married"},
        "borrowers_address_attributes"=>{"0"=>{
            "street_address"=>"xxxx Mission Street",
            "city"=>"San Francisco",
            "state"=>"CA",
            "zip"=>xxxxx
         }
        }
      }
    }}
    

    【讨论】:

    • 不走运。另外,为什么要包含“0”?
    • 这就是它在具有嵌套属性@LouisCruz 的表单上生成的方式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多