سؤال

I've been trying to put a nested form to work for a while and I can't understand why it doesn't show up. Am I missing something?

Here is what I've got:

class User < ActiveRecord::Base
  belongs_to :pharmacy
end

-

class Pharmacy < ActiveRecord::Base
  has_many :users
  accepts_nested_attributes_for :users
end

-

def new
    @pharmacy = Pharmacy.new
    @pharmacy.users.build
end

-

 def pharmacy_params
      params.require(:pharmacy).permit(:name, :address, 
                                        {users: [:id, :name, :email, :password, 
                                                 :password_confirmation]})
 end

-

 <%= form_for @pharmacy do |f| %>
        <div class="field">
            <%= f.label :name, "Name*:" %><br>
            <%= f.text_field :name %>
        </div>
        <div class="field">
            <%= f.label :address, "Address:" %><br>
            <%= f.text_field :address %>
        </div>
        <% f.fields_for :users do |s| %>
            <div class="field">
              <%= s.label :name, "Name*:" %><br>
              <%= s.text_field :name %>
            </div>
            <div class="field">
              <%= s.label :email, "E-mail*:" %><br>
              <%= s.text_field :email %>
            </div>
            <div class="field">
              <%= s.label :password, "Password*:" %>
              <br>
              <%= s.password_field :password %>
            </div>
            <div class="field">
              <%= s.label :password_confirmation, "Confirm password*:" %>
              <br>
              <%= s.password_field :password_confirmation %>
            </div>
        <% end %>
        <div class="actions">
            <%= f.submit "Create" %>
        </div>
  <% end %>

The for loads without throwing any exception but it only shows the Pharmacy fields. It's like if the :users were empty, doesn't show its fields.

هل كانت مفيدة؟

المحلول

<%= f.fields_for :users do |s| %>

You need to add the = to show the users fields.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top