date_select

date_select(object_name, method, options = {}, html_options = {})
Instance Public methods

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).

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_two_digit_numbers - Set to true if you want to display two digit month and day numbers (e.g. â02â instead of âFebruaryâ and â08â instead of â8â).

  • :use_short_month - Set to true if you want to use abbreviated month names instead of full month names (e.g. âFebâ instead of âFebruaryâ).

  • :add_month_numbers - Set to true if you want to use both month numbers and month names (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' i18n functionality for this.

  • :month_format_string - Set to a format string. The string gets passed keys :number (integer) and :name (string). A format string would be something like â%{name} (%<number>02d)â for example. See Kernel.sprintf for documentation on format sequences.

  • :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 Date.today.year - 5if you are creating new record. While editing existing record, :start_year defaults to the current selected year minus 5.

  • :end_year - Set the end year for the year select. Default is Date.today.year + 5 if you are creating new record. While editing existing record, :end_year defaults to the current selected year plus 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 to 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.

  • :selected - Set a date that overrides the actual value.

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

  • :prompt - Set to true (for a generic prompt), a prompt string or a hash of prompt strings for :year, :month, :day, :hour, :minute and :second. Setting this option prepends a select option with a generic prompt (Day, Month, Year, Hour, Minute, Seconds) or the given prompt string.

  • :with_css_classes - Set to true if you want assign different styles for 'select' tags. This option automatically set classes 'year', 'month', 'day', 'hour', 'minute' and 'second' for your 'select' tags.

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.

# 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,
# with the year in the year drop down box starting at 1995.
date_select("article", "written_on", start_year: 1995)

# Generates a date select that when POSTed is stored in the article 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("article", "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 article variable, in the written_on attribute,
# with two digit numbers used for months and days.
date_select("article", "written_on", use_two_digit_numbers: true)

# Generates a date select that when POSTed is stored in the article variable, in the written_on attribute
# with the fields ordered as day, month, year rather than month, day, year.
date_select("article", "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 article variable, in the written_on attribute
# which is initially set to the date 3 days from the current date
date_select("article", "written_on", default: 3.days.from_now)

# Generates a date select that when POSTed is stored in the article variable, in the written_on attribute
# which is set in the form with todays date, regardless of the value in the Active Record object.
date_select("article", "written_on", selected: Date.today)

# 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 })

# Generates a date select with custom prompts.
date_select("article", "written_on", prompt: { day: 'Select day', month: 'Select month', year: 'Select year' })

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.

doc_ruby_on_rails
2015-06-20 00:00:00
Comments
Leave a Comment

Please login to continue.