ruby-on-rails – accepts_nested_attributes_for停止我的子表单呈现?

我有两个模型,业务和地址之间的关联.企业有registered_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

没有显示任何内容,只是一个空的字段集.

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

谁能看到我做错了什么?

谢谢

在控制器中写入此操作(我认为是新的)

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
相关文章
相关标签/搜索