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

问题描述 投票:0回答:1

我想在[]的下拉菜单中显示假期的日期>

_ 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)

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

这是index.html.haml中的systemHoliday视图:

    %table.data#system_holidays
      %thead
        %tr
          %th= check_box_tag('select', 1, false, id: "checkMaster", name: "checkMaster", onClick: "checkAllByName('checkMaster','system_holidays[]');")
          %th
          %th
          %th Name
          %th Date
      %tbody
        - SystemHoliday.all.each do |system_holiday|
          %tr
            %td= system_holiday.name
            %td= system_holiday.to_s

_ holiday_time_range_fields.html.haml:

%tr#time_range.fields
  %td= f.link_to_remove fa_icon("remove 2x")
  %td= f.collection_select :month, Schedule::MONTHS, :last, :first, hide_label: true
  %td= f.collection_select :day_of_month, Schedule::DAYS_OF_MONTH, :to_s, :to_s, hide_label: true

我的表单视图:

= 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
            %th Office Mode
        %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
  %td= f.collection_select :mode_id, @location.modes,:id, :name, include_blank: "-- Select Mode --", hide_label: true

我想在_scheduled_holiday_fields.html.haml%td = f.collection_select:holiday_id,SystemHoliday.all + TenantHoliday.where(tenant:@tenant),:...]内的下拉菜单中显示假期日期。

ruby-on-rails ruby forms drop-down-menu haml
1个回答
0
投票

https://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select

Add the method name_with_date to models
class SystemHoliday < ActiveRecord::Base
  def name_with_date
    "#{name} (#{month} #{day_of_month})"
  end
end

class TenantHoliday < ActiveRecord::Base
  def name_with_date
    "#{name} (#{month} #{day_of_month})"
  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
© www.soinside.com 2019 - 2024. All rights reserved.