我创建了一个Address具有多态关联模型,我想救它,虽然客户端模型嵌套的属性,但我得到Address addressable must exist的@client.errors.
楷模:
class Client < ApplicationRecord
has_one :address, as: :addressable, dependent: :destroy
accepts_nested_attributes_for :address, :allow_destroy => true
end
class Address < ApplicationRecord
belongs_to :addressable, polymorphic: true
end
Run Code Online (Sandbox Code Playgroud)
控制器:
class ClientsController < ApplicationController
def new
@client = Client.new
@client.create_address
end
def create
@client = Client.new(client_params)
if @client.save
...
else
...
end
end
private
def client_params
params.require(:client).permit(:first_name ,:last_name, :company, address_attributes: [:line1, :line2, :line3, :city, :state_province, :postal_code, :country])
end
end
Run Code Online (Sandbox Code Playgroud)