开发者

drop down list in rails

开发者 https://www.devze.com 2023-01-09 23:28 出处:网络
I have something basic generated from nifty_scaffold in a partial form: _form.html.erb <p> <%= f.label :group_id %><br />

I have something basic generated from nifty_scaffold in a partial form: _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 wil开发者_开发知识库l be populated with groups which I set below.

My new action in Employee controller looks like this:

  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

Additionally, how will edit action work? 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.


<%=  select("employee", "group_id", Group.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true })%>

works!

0

精彩评论

暂无评论...
验证码 换一张
取 消