vendredi 27 mai 2016

Rails using helper method in form_tag select_tag

I have a view in my Rails app where I'm wanting to select a unit to dispatch to a call. Here is what it currently looks like.

   <%= form_tag dispatch_call_call_path(call), :class => "dispatch-form-tag" do  %>
   <%= select_tag(:unit_ids, options_from_collection_for_select(Unit.active.order("unit_name ASC"), "id", "unit_name"), :include_blank => true, :required => true, :class => 'select' )%>
   <%= button_tag 'Dispatch', class: 'btn btn-danger btn-small', data: {disable_with: "<i class='icon-spinner'></i>Processing..."} %>
   <% end %>

This works fine and dandy but I want to display more information about the unit in the dropdown such as whether or not they are on a call and their status.

I wrote this helper under calls_helper

def unit_select
   Unit.active.order("unit_name").map{|unit| unit.calls.where(call_status: "open").empty? ? ["#{unit.unit_name} #{unit.unit_type.unit_type}", unit.id] : ["#{unit.unit_name} (on call) #{unit.unit_type.unit_type}", unit.id] }
 end

Which when used in a form gives me Unit name (on call) Unit type if the unit is on a call.

When I am creating a new call from scratch this is how I use it in the form.

      <%= f.select(:unit_ids, unit_select,  {}, {:multiple => true, :class => 'select'}) %>

I want to use this same helper in my index view so when a unit is dispatched to a call the user sees the same information that they would when they are creating a call.

But I'm not sure how to use my unit_select helper in a select_tag. I've read the API docs for Rails but can't seem to figure this one out.

Any help is greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire