instance method date_select

Ruby on Rails 2.2.3

Since v2.2.3

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

date_select(object_name, method, options = {}, html_options = {})

Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based attribute (identified by method) on an object assigned to the template (identified by object). You can the output in the options hash.

Options

  • :use_month_numbers - Set to true if you want to use month numbers rather than month names (e.g.

    "2" instead of "February").
  • :use_short_month - Set to true if you want to use the abbreviated month name instead of the full name (e.g. “Feb” instead of “February”).

  • :add_month_number - Set to true if you want to show both, the month’s number and name (e.g. “2 - February” instead of “February”).

  • :use_month_names - Set to an array with 12 month names if you want to customize month names. Note: You can also use Rails’ new i18n functionality for this.

  • :date_separator - Specifies a string to separate the date fields. Default is “” (i.e. nothing).

  • :start_year - Set the start year for the year select. Default is Time.now.year - 5.

  • :end_year - Set the end year for the year select. Default is Time.now.year + 5.

  • :discard_day - Set to true if you don’t want to show a day select. This includes the day as a hidden field instead of showing a select field. Also note that this implicitly sets the day to be the first of the given month in order to not create invalid dates like 31 February.

  • :discard_month - Set to true if you don’t want to show a month select. This includes the month as a hidden field instead of showing a select field. Also note that this implicitly sets :discard_day to true.

  • :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.

  • :order - Set to an array containing :day, :month and :year do customize the order in which the select fields are shown. If you leave out any of the symbols, the respective select will not be shown (like when you set :discard_xxx => true. Defaults to the order defined in the respective locale (e.g. [:year, :month, :day] in the en locale that ships with Rails).

  • :include_blank - Include a blank option in every select field so it’s possible to set empty dates.

  • :default - Set a default date if the affected date isn’t set or is nil.

  • :disabled - Set to true if you want show the select fields as disabled.

If anything is passed in the html_options hash it will be applied to every select tag in the set.

NOTE: Discarded selects will default to 1. So if no month select is available, January will be assumed.

Examples

# Generates a date select that when POSTed is stored in the post variable, in the written_on attribute
date_select("post", "written_on")

# Generates a date select that when POSTed is stored in the post variable, in the written_on attribute,
# with the year in the year drop down box starting at 1995.
date_select("post", "written_on", :start_year => 1995)

# Generates a date select that when POSTed is stored in the post variable, in the written_on attribute,
# with the year in the year drop down box starting at 1995, numbers used for months instead of words,
# and without a day select box.
date_select("post", "written_on", :start_year => 1995, :use_month_numbers => true,
                                  :discard_day => true, :include_blank => true)

# Generates a date select that when POSTed is stored in the post variable, in the written_on attribute
# with the fields ordered as day, month, year rather than month, day, year.
date_select("post", "written_on", :order => [:day, :month, :year])

# Generates a date select that when POSTed is stored in the user variable, in the birthday attribute
# lacking a year field.
date_select("user", "birthday", :order => [:month, :day])

# Generates a date select that when POSTed is stored in the user variable, in the birthday attribute
# which is initially set to the date 3 days from the current date
date_select("post", "written_on", :default => 3.days.from_now)

# Generates a date select that when POSTed is stored in the credit_card variable, in the bill_due attribute
# that will have a default day of 20.
date_select("credit_card", "bill_due", :default => { :day => 20 })

The selects are prepared for multi-parameter assignment to an Active Record object.

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 all month choices are valid.

Parameters

object_name req
method req
options opt = {}
html_options opt = {}
Source
# File actionpack/lib/action_view/helpers/date_helper.rb, line 178
      def date_select(object_name, method, options = {}, html_options = {})
        InstanceTag.new(object_name, method, self, options.delete(:object)).to_date_select_tag(options, html_options)
      end

Defined in actionpack/lib/action_view/helpers/date_helper.rb line 178 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionView::Helpers::DateHelper

Type at least 2 characters to search.

↑↓ navigate · open · esc close