dgosh
dgosh

Reputation: 15

How to show date in f.collection_select in Rails

I want to show the date of holidays in a dropdown menu within the

_scheduled_holiday_fields.html.haml

%td= f.collection_select :holiday_id, SystemHoliday.all + TenantHoliday.where(tenant: @tenant),:id, :name, include_blank: "-- Select Holiday --", hide_label: true

Currently in the dropdown menu it shows as:

Holiday      
Christmas    

In the dropdown menu I want to show as:

Holiday
Christmas (Dec 25)

I have a separate page for a person to enter the holidays they observe as well as the date for the holiday.

my form view:

= bootstrap_nested_form_for(@schedule,bsf_opts) do |f|
  .section
    .section_header Scheduled Holidays
    .panel.panel-primary
      .panel-body Select the holidays observed by your company as well as the office mode for each holiday.
      %table.data#scheduled_holidays_table
        %thead
          %tr
            %th
            %th Holiday
        %tbody
          = f.fields_for :scheduled_holidays, wrapper: false
      .panel-footer
        .center= f.link_to_add "Add Holiday", :scheduled_holidays, class: :button, data: {target: "#scheduled_holidays_table"}

_scheduled_holiday_fields.html.haml:

%tr.fields
  %td= f.collection_select :holiday_id, SystemHoliday.all + TenantHoliday.where(tenant: @tenant),:id, :name, include_blank: "-- Select Holiday --", hide_label: true

Upvotes: 0

Views: 86

Answers (1)

Feifei Xiong
Feifei Xiong

Reputation: 446

Refer to "collection_select":

# Add attr_accessor and collection_select text_method to Holiday
class Holiday < ActiveRecord::Base

  def name_with_date
    "#{name} (#{to_s})"
  end
end


# Then modify the form input
%td= f.collection_select :holiday_id, SystemHoliday.all + TenantHoliday.where(tenant: @tenant), :id, :name_with_date, include_blank: "-- Select Holiday --", hide_label: true

Upvotes: 1

Related Questions