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

Description

Creates hour and minute <select> elements, plus hidden <input> elements for the year, month and day, bound to a model attribute.

Arguments

Changing a value updates the ERB and HTML.

object_name:user
method:starts_at
This attribute determines the following:
  • name: (4i) (hour) and (5i) (minute) are appended, as in user[starts_at(4i)]. Active Record combines the values it receives into one date and time and assigns it to the attribute.
  • selected: added to the <option> elements whose value matches the object's value (the current date and time if the object has no value). If the object has no value and include_blank or prompt is given, no <option> gets it.
  • Year, month and day: hidden <input> elements with (1i) to (3i). If the object has no value and include_blank or prompt is given, they are all 1.
options= {}
true omits the hidden year, month and day <input> elements.

Options for the time <select> elements

true adds a <select> for seconds ((6i)) after the minute <select>.
15
Fills the minute <select> with minutes in steps of this number. If the selected minute is not one of them, no <option> is selected.
9
The first hour in the hour <select>.
17
The last hour in the hour <select>.
true formats the hour <option> text as 12-hour time with AM and PM. The value stays in 24-hour time.
"h"
Puts this string between the hour and minute <select> elements, and also between the minute and seconds ones. The string is not escaped (character references such as &mdash; go into the HTML as is).
true adds a hidden <input> instead of the hour <select>.
true replaces the minute <select> with a hidden <input>. In datetime_select and select_datetime, the seconds from include_seconds: true also become 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.
Time.new(2000, 1, 2, 18, 0, 0)
The date and time to mark as selected when the object has no value. Ignored when the :include_blank or :prompt option is given. A hash ({ hour: 18, minute: 0 }) fills any parts you leave out, such as the year and day, from the current date and time.
Time.new(2000, 1, 2, 18, 0, 0)
The date and time 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 every <select>; 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