:bus-6ren">
gpt4 book ai didi

ruby-on-rails - accepts_nested_attributes_for 停止我的子表单渲染?

转载 作者:行者123 更新时间:2023-12-04 06:20:45 25 4
gpt4 key购买 nike

我有 2 个模型之间的关联,Business 和 Address。企业有注册地址的地方。我这样做了如下。

class Business < ActiveRecord::Base  
has_one :registered_address, :class_name => "Address", :foreign_key => :business_registered_address_id
accepts_nested_attributes_for :registered_address
end

class Address < ActiveRecord::Base
belongs_to :business
end

这个关联对我来说很好用。当我使用以下方法渲染表单时:
= form_for @business do |form|
= form.inputs :name => "Registered address" do
= form.fields_for :registered_address do |address|
= address.input :postcode
= address.input :line_1
= address.input :line_2
= address.input :line_3
= address.input :town
= address.input :county

什么都没有显示,只是一个空的字段集。

当我注释掉业务模型中的 accepts_nested_attributes_for 行时,它会正确显示(但不保存)所有字段。

谁能看到我做错了什么?

谢谢

最佳答案

在您的 Controller 中写入此操作(我认为 new)

def new
@business = Business.new
@business.build_registered_address
...
end

或以您的形式 @business.registered_address.new
= form_for @business do |form|
= form.inputs :name => "Registered address" do
= form.fields_for :registered_address, @business.registered_address.new do |address|
= address.input :postcode
= address.input :line_1
= address.input :line_2
= address.input :line_3
= address.input :town
= address.input :county

关于ruby-on-rails - accepts_nested_attributes_for 停止我的子表单渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5312367/

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