我正在尝试保存一个深层嵌套的表单。有三种不同的模型:应用程序,申请人和地址。地址是多态的,因为应用程序和申请人都可以拥有它们。这些是我发送的参数:
self.imageView.contentMode
如果在我的请求中发送此消息,我收到以下回复:
UIButton
以下错误消息:
{"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
}
}
}}
为什么我无法设置嵌套属性?我需要修复什么来使其工作?
以下是我的相关文件:
application.rb中
<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>
Applicant.rb
@messages={:"borrower.address.addressable"=>["must exist"]}, @details={"borrower.address.addressable"=>[{:error=>:blank}]}
Address.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
Applications_Controller.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
答案 0 :(得分:7)
发现了这个问题。将optional: true
添加到Address
belongs_to
后,情况正常。这是一个新的Rails 5约定。我记得把它放在我的coborrower上,但不是那里。希望这有助于某人!
答案 1 :(得分: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"=>"test@me.com",
"marital_status"=>"married"},
"borrowers_address_attributes"=>{"0"=>{
"street_address"=>"xxxx Mission Street",
"city"=>"San Francisco",
"state"=>"CA",
"zip"=>xxxxx
}
}
}
}}