2016-03-02 4 views
0

У меня есть 2 модели:вложенные формы не работает с HAS_ONE ассоциации и fields_for

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> 

Когда я запускаю код, на дисплее отображаются только текстовые поля full_name, адрес электронной почты, телефон. он не отображает поля в fields_for.

Как отображать поля в fields_for когда созданная модель использует вложенную модель has_one отношение?

ответ

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

Попробуйте

+0

спасибо. он работает: D –

Смежные вопросы