如何在Rails中的date_select中设置默认日期和include_blank

jmv*_*bxx 3 ruby-on-rails

我在Rails 4中使用date_select,我希望能够将今天的日期作为默认日期,但也允许用户将日期字段留空.

<%= f.date_select(:birthdate, {include_blank: true, default: Date.today.to_date, start_year: Date.today.year - 100, end_year: Date.today.year - 18}) %>
Run Code Online (Sandbox Code Playgroud)

当页面打开时,默认情况下允许空白以及今天的日期的最佳方式是什么?

谢谢!

ahm*_*eod 9

您需要的选项是selected,即:

<%= f.date_select(:birthdate,
                  include_blank: true,
                  selected: Date.today, # substitute 18.years.ago to prefill year
                  start_year: Date.today.year - 100,
                  end_year: Date.today.year - 18) %>
Run Code Online (Sandbox Code Playgroud)