Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails select_tag/dropdown with enums

I'm got a model Lead which has a field Status which consists of multiple values via an enum.

enum status: { open: 0, closed: 1, qualified: 2, rejected: 3 }

I'm trying to create a select-field (in a table) where the new status will be saved via ajax. Can someone help me to create the select_tag, online I can find enum's which work but because I have both ID's and values it's a bit more complicated.

At this moment I have something like this, though it doesn't work:

  <tbody>
    <% @leads.each do |lead| %>
    <tr class="<%=cycle('odd', 'even') %> location_row" id="lead_row" data-id="<%= lead.id%>">
      <td><%= lead.id %></td>
      <td><%= lead.fullname %></td>
      <td><%= lead.email %></td>
      <td><%= lead.phone %></td>
      <td><%= select_tag :Status, Lead.statuses.keys.to_a %></td> #trying this, without luck
      <td><%= select_tag :Status, Lead.statuses.keys.to_a.map { |w, v| [w.titleize, v] }%></td> #2nd try, without luck
      <td><%= link_to (fa_icon "pencil-square-o "), edit_lead_path({:id => lead.id, :first_last_name => lead.first_last_name}), :title => 'Edit Lead', :class => "action-button" %></td>
    </tr>
    <% end %>
  </tbody>

Thanks, T

like image 489
Thomas C Avatar asked Jun 07 '16 08:06

Thomas C


1 Answers

I found the answer myself by trial-and-error.

<%= select_tag :status, options_for_select(Lead.statuses.map {|k, v| [k.humanize.capitalize, v]}) %>
like image 110
Thomas C Avatar answered Sep 18 '22 18:09

Thomas C