gpt4 book ai didi

ruby-on-rails - Rails 5 中具有多态关联的嵌套属性

转载 作者:行者123 更新时间:2023-12-03 15:59:50 25 4
gpt4 key购买 nike

我创建了一个 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

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

最佳答案

 belongs_to :xx, polymorphic: true, optional: true

关于ruby-on-rails - Rails 5 中具有多态关联的嵌套属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37093554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com