嵌套表单不适用于 has_one 关联和 fields_for

nested forms not working with has_one association and fields_for

我有 2 个模型:

class Buyer < ActiveRecord::Base
  has_one :buyer_info
  accepts_nested_attributes_for :buyer_info
end

型号BuyerInfo

class BuyerInfo < ActiveRecord::Base
  belongs_to :buyer
end

在控制器中(我使用设计来创建买家/buyers/registrations_controller

class Buyers::RegistrationsController < Devise::RegistrationsController
  def new
    super
    resource.build_buyer_info
  end
end

形式为registrations/_form

<div class="">
  <div class="mid">
    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
      <%= f.text_field :full_name, :placeholder => "YOUR NAME", :class => "form-control"%>
      <%= f.text_field :email, :placeholder => "EMAIL ADDRESS", :class => "form-control", :class => "form-control"%>
      <%= f.text_field :phone, :placeholder => "PHONE NUMBER", :class => "form-control"%>
      <br/> <br/>

      <%= f.fields_for :buyer_info do |buyer_info| %>
        <%= buyer_info.text_field :email_receive1, :placeholder => "SEND MY LEADS TO EMAIL #1 ", :class => "form-control"%>
        <%= buyer_info.text_field :email_receive2, :placeholder => "SEND MY LEADS TO EMAIL #2 ", :class => "form-control"%>
        <%= buyer_info.text_field :email_receive3, :placeholder => "SEND MY LEADS TO EMAIL #3 ", :class => "form-control"%>
        <%= buyer_info.text_field :email_receive4, :placeholder => "SEND MY LEADS TO EMAIL #4 ", :class => "form-control"%>  
      <% end %>
      <%= f.submit "Comment", :type => :image, :src => "#{asset_url(@source_folder+'signup-button.png')}" %>

    <% end %>       
  </div>           
</div>  

当我运行编码时,在视图中只显示text_fieldsfull_name,电子邮件,phone。它不显示 fields_for.

中的字段

如何在创建的模型使用嵌套模型 has_one 关系时显示 fields_for 中的字段?

class Buyers::RegistrationsController < Devise::RegistrationsController
  def new
    build_resource({})
    @buyer_info = resource.build_buyer_info
  end
end

试试这个