如何在 Rails 中显示 f.collection_select 中的日期

How to show date in f.collection_select in Rails

我想在

的下拉菜单中显示假期日期

_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

当前在下拉菜单中显示为:

Holiday      
Christmas    

在下拉菜单中我想显示为:

Holiday
Christmas (Dec 25)

我有一个单独的页面供人们输入他们观察的假期以及假期的日期。

我的表单视图:

= 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

参考“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