module ActionView::Helpers::DateHelper
def date_select(object_name, method, options = {}, html_options = {})
Note: If the day is not included as an option but the month is, the day will be set to the 1st to ensure that
The selects are prepared for multi-parameter assignment to an Active Record object.
date_select("article", "written_on", day_format: ->(day) { day.ordinalize })
# Generates a date select with custom day format.
date_select("article", "written_on", year_format: ->(year) { "Heisei #{year - 1988}" })
# Generates a date select with custom year format.
date_select("article", "written_on", prompt: { day: 'Select day', month: 'Select month', year: 'Select year' })
# Generates a date select with custom prompts.
date_select("credit_card", "bill_due", default: { day: 20 })
# that will have a default day of 20.
# Generates a date select that when POSTed is stored in the credit_card variable, in the bill_due attribute
date_select("article", "written_on", selected: Date.today)
# which is set in the form with today's date, regardless of the value in the Active Record object.
# Generates a date select that when POSTed is stored in the article variable, in the written_on attribute
date_select("article", "written_on", default: 3.days.from_now)
# which is initially set to the date 3 days from the current date
# Generates a date select that when POSTed is stored in the article variable, in the written_on attribute
date_select("user", "birthday", order: [:month, :day])
# lacking a year field.
# Generates a date select that when POSTed is stored in the user variable, in the birthday attribute
date_select("article", "written_on", order: [:day, :month, :year])
# with the fields ordered as day, month, year rather than month, day, year.
# Generates a date select that when POSTed is stored in the article variable, in the written_on attribute
date_select("article", "written_on", use_two_digit_numbers: true)
# with two digit numbers used for months and days.
# Generates a date select that when POSTed is stored in the article variable, in the written_on attribute,
discard_day: true, include_blank: true)
date_select("article", "written_on", start_year: 1995, use_month_numbers: true,
# and without a day select box.
# with the year in the year drop down box starting at 1995, numbers used for months instead of words,
# Generates a date select that when POSTed is stored in the article variable, in the written_on attribute,
date_select("article", "written_on", start_year: 1995)
# with the year in the year drop down box starting at 1995.
# Generates a date select that when POSTed is stored in the article variable, in the written_on attribute,
date_select("article", "written_on")
# Generates a date select that when POSTed is stored in the article variable, in the written_on attribute.
NOTE: Discarded selects will default to 1. So if no month select is available, January will be assumed.
If anything is passed in the +html_options+ hash it will be applied to every select tag in the set.
* :use_hidden - Set to true if you only want to generate hidden input tags.
will extend the select type with the given value. Use +html_options+ to modify every select tag in the set.
strings for :year, :month, :day, :hour, :minute, :second
select tags. This automatically set classes 'year', 'month', 'day', 'hour', 'minute' and 'second'. A hash of
* :with_css_classes - Set to true or a hash of strings. Use true if you want to assign generic styles for
or the given prompt string.
Setting this option prepends a select option with a generic prompt (Day, Month, Year, Hour, Minute, Seconds)
for :year, :month, :day, :hour, :minute and :second.
* :prompt - Set to true (for a generic prompt), a prompt string or a hash of prompt strings
* :disabled - Set to true if you want show the select fields as disabled.
* :selected - Set a date that overrides the actual value.
* :default - Set a default date if the affected date isn't set or is +nil+.
dates.
* :include_blank - Include a blank option in every select field so it's possible to set empty
the respective locale (e.g. [:year, :month, :day] in the en locale that ships with Rails).
select will not be shown (like when you set discard_xxx: true. Defaults to the order defined in
customize the order in which the select fields are shown. If you leave out any of the symbols, the respective
* :order - Set to an array containing :day, :month and :year to
as a hidden field instead of showing a select field.
* :discard_year - Set to true if you don't want to show a year select. This includes the year
as a hidden field instead of showing a select field. Also note that this implicitly sets :discard_day to true.
* :discard_month - Set to true if you don't want to show a month select. This includes the month
first of the given month in order to not create invalid dates like 31 February.
as a hidden field instead of showing a select field. Also note that this implicitly sets the day to be the
* :discard_day - Set to true if you don't want to show a day select. This includes the day
* :day_format - Set format of days for day select. Lambda should be passed.
* :year_format - Set format of years for year select. Lambda should be passed.
the current selected year plus 5.
you are creating new record. While editing existing record, :end_year defaults to
* :end_year - Set the end year for the year select. Default is Date.today.year + 5 if
the current selected year minus 5.
you are creating new record. While editing existing record, :start_year defaults to
* :start_year - Set the start year for the year select. Default is Date.today.year - 5 if
* :datetime_separator- Specifies a string to separate the date and time fields. Default is " — ".
* :time_separator - Specifies a string to separate the time fields. Default is " : ".
* :date_separator - Specifies a string to separate the date fields. Default is "" (i.e. nothing).
See Kernel.sprintf for documentation on format sequences.
and +:name+ (string). A format string would be something like "%{name} (%
* :month_format_string - Set to a format string. The string gets passed keys +:number+ (integer)
Note: You can also use Rails' i18n functionality for this.
* :use_month_names - Set to an array with 12 month names if you want to customize month names.
"2 - February" instead of "February").
* :add_month_numbers - Set to true if you want to use both month numbers and month names (e.g.
month names (e.g. "Feb" instead of "February").
* :use_short_month - Set to true if you want to use abbreviated month names instead of full
"02" instead of "February" and "08" instead of "8").
* :use_two_digit_numbers - Set to true if you want to display two digit month and day numbers (e.g.
"2" instead of "February").
* :use_month_numbers - Set to true if you want to use month numbers rather than month names (e.g.
==== Options
attribute (identified by +method+) on an object assigned to the template (identified by +object+).
Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based
def date_select(object_name, method, options = {}, html_options = {}) Tags::DateSelect.new(object_name, method, self, options, html_options).render end