Ruby on rails 接受\u嵌套的\u属性\u并选择标记
我有一个房间模型和一个人模型 一个房间可以有很多人,一个人可以有一个房间 在房间创建屏幕上,我可以将n个人“链接”到此新房间 所以我想要一个可变数量的select标签,它包含所有人的列表 我不知道如何构建select标记 有人能帮忙吗 谢谢 我有以下联想Ruby on rails 接受\u嵌套的\u属性\u并选择标记,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我有一个房间模型和一个人模型 一个房间可以有很多人,一个人可以有一个房间 在房间创建屏幕上,我可以将n个人“链接”到此新房间 所以我想要一个可变数量的select标签,它包含所有人的列表 我不知道如何构建select标记 有人能帮忙吗 谢谢 我有以下联想 class Room < ActiveRecord::Base has_many :people accepts_nested_attributes_for :people end class Person < Activ
class Room < ActiveRecord::Base
has_many :people
accepts_nested_attributes_for :people
end
class Person < ActiveRecord::Base
belongs_to :room
end
教室
我使用一个分部来构建房间/新表单
<% form_for(@room) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :date %><br />
<%= f.date_select :date %>
</p>
<% f.fields_for :people do |builder| %>
<p>
<%= builder.label :person_id, "Person" %><br />
<%= select 'room', 'people', Person.all.collect{|person| [person.name, person.id]}%>
</p>
<% end %>
<p>
<%= f.label :comment %><br />
<%= f.text_area :comment %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
查看该模块。它提供了collection\u select方法
这部分内容符合您的要求:
<% form_for(@room) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :date %><br />
<%= f.date_select :date %>
</p>
<p>
<%= f.label :people_ids, "Person" %><br />
<%= collection_select :people_ids, Person.all, :name, :id, {:multiple => true} %>
</p>
<p>
<%= f.label :comment %><br />
<%= f.text_area :comment %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
真}%>
查看该模块。它提供了collection\u select方法
这部分内容符合您的要求:
<% form_for(@room) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :date %><br />
<%= f.date_select :date %>
</p>
<p>
<%= f.label :people_ids, "Person" %><br />
<%= collection_select :people_ids, Person.all, :name, :id, {:multiple => true} %>
</p>
<p>
<%= f.label :comment %><br />
<%= f.text_area :comment %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
真}%>
不错。。。快速回答,谢谢
下面是经过测试的正确代码
<% f.fields_for :people do |builder| %>
<p>
<%= builder.label :person_id, "Person" %><br />
<%= builder.collection_select :person_id, Person.all, :id, :name, {:multiple => true} %>
</p>
<% end %>
真}%>
不错。。。快速回答,谢谢
下面是经过测试的正确代码
<% f.fields_for :people do |builder| %>
<p>
<%= builder.label :person_id, "Person" %><br />
<%= builder.collection_select :person_id, Person.all, :id, :name, {:multiple => true} %>
</p>
<% end %>
真}%>