【问题标题】:Custom date_select values in Rails?Rails 中的自定义 date_select 值?
【发布时间】:2019-12-07 00:47:42
【问题描述】:

我想更改 date_select 中的月份选择输入:更改数组的值(将一月、二月...改为 Janvier、Février...)。而且我还想为每个选择框(日、月和年)附加样式。这是我的日期选择

<%= f.date_select :birthdate, 
                 order: [:day, :month, :year],
                 prompt: { day: 'Jour', month: 'Mois', year: 'Année' }, 
                 start_year: Date.today.year - 13,
                 end_year: Date.today.year - 100 
%>

我似乎没有在 Rails 官方文档中找到任何内容。有什么帮助吗?谢谢:)

【问题讨论】:

  • Date.today.year - 13 == 13.years.ago.year
  • 它不会因为不太可能而歧视 100 岁以上的人。

标签: ruby-on-rails ruby forms date


【解决方案1】:

我想下面的代码会对你有所帮助。

<%= f.date_select :birthdate,
                 {
                 order: [:day, :month, :year],
                 prompt: { day: 'Jour', month: 'Mois', year: 'Année' }, 
                 start_year: Date.today.year - 13,
                 end_year: Date.today.year - 100,
                 locale: :fr
                 },
                {class: 'custom-style'}

%>

【讨论】:

    【解决方案2】:

    我猜设置f.date_select ... locale: :fr 会有所帮助

    【讨论】:

      【解决方案3】:

      使用use_month_names 选项设置月份的自定义值:

      <%= f.date_select :birthdate, 
            {
              order: [:day, :month, :year],
              prompt: { day: 'Jour', month: 'Mois', year: 'Année' }, 
              start_year: Date.today.year - 13,
              end_year: Date.today.year - 100,
              use_month_names: ["Janvier", "Février", etc..]
             },
             { class: "some-css-class-for-styling" }
      %>
      

      【讨论】:

        猜你喜欢
        • 2012-08-29
        • 2014-04-15
        • 1970-01-01
        • 2013-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-31
        • 1970-01-01
        相关资源
        最近更新 更多