Drop-down list in rails

I have something basic generated from nifty_scaffoldin 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 a text field to a drop-down list that will be populated groups, which I set below.

My newaction in the controller is Employeeas follows:

  def new
    @employee = Employee.new
    @groups = Group.all
  end

How to create a drop-down list where it will be filled with all groups in a variable @groups

In addition, how will the action work edit? there I want the assigned group to be preselected. Since I use a partial form, editthe same form will be used.

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

!

+2

All Articles