Reputation: 73
I have 2 models:
class Buyer < ActiveRecord::Base
has_one :buyer_info
accepts_nested_attributes_for :buyer_info
end
Model BuyerInfo
class BuyerInfo < ActiveRecord::Base
belongs_to :buyer
end
In controller (I use devise to create buyer /buyers/registrations_controller
)
class Buyers::RegistrationsController < Devise::RegistrationsController
def new
super
resource.build_buyer_info
end
end
In _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>
When I run code, in view only display text_fields full_name, email, phone. it dont display fields in fields_for.
How to display fields in fields_for when created model uses nested model has_one relation?
Upvotes: 0
Views: 587
Reputation: 3803
class Buyers::RegistrationsController < Devise::RegistrationsController
def new
build_resource({})
@buyer_info = resource.build_buyer_info
end
end
Try this
Upvotes: 1