具有强参数的嵌套表单无效

时间:2014-03-22 16:13:51

标签: ruby-on-rails-4 nested-forms

我一直试图将嵌套表单暂停工作一段时间,但我无法理解为什么它不能显示出来。我错过了什么吗?

以下是我所得到的:

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 %>

for加载时不会抛出任何异常,但只显示Pharmacy字段。如果:users为空,则不会显示其字段。

1 个答案:

答案 0 :(得分:0)

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

您需要添加=以显示用户字段。