我有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
在_form 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_fields full_name,email,phone。它不显示fields_for中的字段。
当创建的模型使用嵌套模型has_one relation?
时,如何在fields_for中显示字段答案 0 :(得分:1)
class Buyers::RegistrationsController < Devise::RegistrationsController
def new
build_resource({})
@buyer_info = resource.build_buyer_info
end
end
试试这个