In the app I'm developing I have a date selector which will mostly be used with the current date as value (or a date a few days later). In order to reduce work for my users, I want to set today's date as default value.
I can easily set the year with:
date_select f, :date, class="form_control", year: [options: @current_year, @current_year+1]
,
but have not managed to do something similar for the month and day.
I've tried changing the code to date_select f, :date, class="form_control", year: [options: @current_year, @current_year+1], month: [default: 2]
,
date_select f, :date, class="form_control", year: [options: @current_year, @current_year+1], month: [default: "2"]
,
date_select f, :date, class="form_control", year: [options: @current_year, @current_year+1], month: [default: "02"]
,
but none of these worked. Replacing default
with value
made no difference. What mistake am I making?
Thanks in advance
Per the docs, the default option is given directly to date_select. So this should work:
date_select f, :date, default: {@current_year, 2, 13}
You can pass the date using other formats too, like a map instead of a tuple. Check datetime_select docs for the accepted formats.
Just in case if anyone is wondering you can also set the default time by doing this
<%= datetime_select f, :start, class: "form-control",
default: {{@current_year, @current_month, @current_day},
{@current_hour, @current_minute, @current_second}} %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With