Rails already provide several form helpers for our ease of use.
The latest addition is weekday_helper
that provides a nice select dropdown
with weekdays.
We can also use weekday_options_for_select
with form.select
.
Before
The above will result in
After
Rails 7 adds weekday_select and weekday_options_for_select helper
to render the select dropdown with weekdays.
weekday_options_for_select:
Let’s look into the weekday_options_for_select
syntax
and
the available options we can pass to it.
Available Options:
selected
NOTE: Provide the correct default based on the values
E.g.
- If the
index_as_value
is set to true the selected value should be between 0
to 6
.
- If
:day_format
is set to :abbr_day_names
default should be set accordingly ("Sun"
, "Mon"
).
:index_as_value
- default:
false
- Returns options with index as value (
0
to 6
)
:day_format
- default:
:day_names
- Pass
:abbr_day_names
for abbreviated week days.
If needed,
we can also add custom days in en.yml
and
language-specific locales.