ActionView::Helpers::DateHelper
def date_select(object_name, method, options = {}, html_options = {})

Description

Creates year, month and day <select> elements bound to a model attribute.

Arguments

Changing a value updates the ERB and HTML.

object_name:user
method:birthday
This attribute determines the following:
  • name: (1i) (year), (2i) (month) and (3i) (day) are appended, as in user[birthday(1i)]. Active Record combines the three values it receives into one date and assigns it to the attribute.
  • selected: added to the <option> elements whose value matches the object's value (today's date if the object has no value). If the object has no value and include_blank or prompt is given, no <option> gets it.
  • The values listed: for the year, the 5 years before and after the selected year (the current year if there is none); for the day, 1 to 31 regardless of the month.
options= {}

Options for the date <select> elements

[:day, :month, :year]
Orders the year, month and day <select> elements as given. Defaults to the order in the date.order translation. Year, month or day parts missing from the array become hidden <input> elements, as with discard_year and the like (without the month, the day also becomes a hidden one with a value of 1).
1980
The first year in the year <select>. Defaults to 5 years before the selected year.
1985
The last year in the year <select>. Defaults to 5 years after the selected year. If it is less than start_year, the years are listed newest first.
true uses numbers instead of names as the month <option> text.
true uses two-digit numbers as the month and day <option> text.
true uses both the number and the name as the month <option> text.
"%{number}: %{name}"
The format of the month <option> text. %{number} is replaced with the number and %{name} with the name.
->(year) { "Year #{year}" }
Calls the proc for each year and uses its return value as the text of that year's <option>.
->(day) { "Day #{day}" }
Calls the proc for each day and uses its return value as the text of that day's <option>.
" / "
Puts this string between the year, month and day <select> elements. The string is not escaped (character references such as &mdash; go into the HTML as is).
true replaces the day <select> with a hidden <input> whose value is 1.
true replaces the month <select> with a hidden <input>. The day <select> is also replaced, with a hidden <input> whose value is 1.
true replaces the year <select> with a hidden <input>.

Options for all <select> elements

true adds an <option> with an empty value at the top of every <select>.
Depending on the value:
  • true: adds an <option> with an empty value at the top of every <select>. Its text comes from translations such as datetime.prompts.year.
  • A string: adds an <option> with the string as its text at the top of every <select>.
  • A hash ({ year: "Year" }): adds one only to the <select> elements named by the keys (the year <select> for year), with the values as their text.
Date.new(2000, 1, 2)
The date to mark as selected when the object has no value. Ignored when the :include_blank or :prompt option is given. With a hash ({ day: 20 }), any part left out, such as the year or month, comes from today's date.
Date.new(2000, 1, 2)
The date to mark as selected instead of the object's value.
true adds disabled to every <select> and hidden <input>.
true adds a class to each <select>, such as class="year" for the year and class="hour" for the hour. A hash ({ year: "y" }) sets the class of each <select> named by a key to its value, and adds class="" to the other <select> elements. If the class html_option is also given, appends to it after a space (a hash or array class is turned into a string as is).
true replaces every <select> with a hidden <input>.
Rails handles options of the same name in almost the same way across all DateHelper helpers. Where one behaves differently, its description says so. locale uses that locale's translations for the month names, the prompt strings and the date.order order. Options that do not change the HTML, and ones this site considers rarely used, are not listed on this page.Options not listed here do not become HTML attributes.
html_options= {}
Common options

Rails handles these options the same way in every helper. Options the HTML spec does not allow on the <select>, and ones this site considers rarely used, are not listed here.

Shared by all helpers

{active: true, hidden: false}
A hash gives the keys whose values are not false or nil, joined with spaces. An array gives its elements joined with spaces, after removing nil, false and empty strings (including whitespace-only strings).
{controller: "tags", tags_suggestions_value: ["ruby", "rails"]}
A hash adds data-* attributes, with _ in the keys converted to -. Array and hash values are converted to JSON strings.
{describedby: ["name_help", "name_error"], invalid: false}
Like :data, adds aria-* attributes. Array values are joined with spaces, leaving out nil and empty strings (nothing is added if none remain). For a hash value, the keys whose values are not false or nil are joined with spaces.
Any other html_options become HTML attributes of all three <select> elements; those with a nil value are left out. id and name are also used as is for every <select>, instead of the generated values.

ERB

Changes from the previous output: Added Removed Changed


          

HTML


          

Preview

Interacting with the preview updates the form submission.

Form submission

Form data

namevalue
Request body

Content-Type:


              

params