我一直试图将嵌套表单暂停工作一段时间,但我无法理解为什么它不能显示出来。我错过了什么吗?
以下是我所得到的:
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
为空,则不会显示其字段。
答案 0 :(得分:0)
<%= f.fields_for :users do |s| %>
您需要添加=
以显示用户字段。