[英]drop down list in rails
I have something basic generated from nifty_scaffold
in a partial form: _form.html.erb
我有一些从
nifty_scaffold
以部分形式生成的基本内容: _form.html.erb
<p>
<%= f.label :group_id %><br />
<%= f.text_field :group_id %>
</p>
Instead of a text field I want to convert the above from text_field to a drop down list which will be populated with groups
which I set below. 取而代之的是文本字段我想上面的转换从text_field到一个下拉列表,它会与被填充
groups
,我设置如下。
My new
action in Employee
controller looks like this: 我在
Employee
控制器中的new
操作如下所示:
def new
@employee = Employee.new
@groups = Group.all
end
How do I make a drop down list where it will be populated with all groups in @groups
variable 如何创建一个下拉列表,其中将填充
@groups
变量中的所有组
Additionally, how will edit
action work? 此外,
edit
操作将如何工作? there I will want the assigned group to be preselected. 在那里,我希望预先选择指定的组。 Since I am using a partial form, same form will be used in
edit
as well. 由于我使用的是部分表单,因此
edit
也会使用相同的表单。
<%= select("employee", "group_id", Group.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true })%>
作品!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.