date_select
- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (0)
- 2.0.3 (38)
- 2.1.0 (4)
- 2.2.1 (34)
- 2.3.8 (13)
- 3.0.0 (-1)
- 3.0.9 (-3)
- 3.1.0 (1)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (21)
- 4.1.8 (13)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (13)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (6)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (5)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
date_select(object_name, method, options = {})
public
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). It’s possible to tailor the selects through the options hash, which accepts all the keys that each of the individual select builders do (like :use_month_numbers for select_month) as well as a range of discard options. The discard options are :discard_year, :discard_month and :discard_day. Set to true, they’ll drop the respective select. Discarding the month select will also automatically discard the day select. It’s also possible to explicitly set the order of the tags using the :order option with an array of symbols :year, :month and :day in the desired order. Symbols may be omitted and the respective select is not included.
Pass the :default option to set the default date. Use a Time object or a Hash of :year, :month, :day, :hour, :minute, and :second.
Passing :disabled => true as part of the options will make elements inaccessible for change.
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.
Get year to show in descending order (Today to 1920 for example)
The way people think of time start and end would be 1920 to today. This made me think “but I want it show the current year first then down.” Well it’s as simple as swapping the start_year and end_year.
date_select :date, :start_year => Date.current.year, :end_year => 1920 # => 2008, 2007, 2006, 2005 ... 1920
Disable default date
If you want a date selector that initially doesn’t have a date selected you can pass it the option :include_blank.
date_select("project", "due_date", :include_blank => true)
Date_select with assert_valid_keys
If you are using date_select with assert_valid_keys you have to allow 3 parameters named field(1i), field(2i) and field(3i).
For example with field
date_select("post", "written_on")
You have to allow following fields:
params[:post].assert_valid_keys( 'written_on(1i)', 'written_on(2i)', 'written_on(3i)' )
end_year
date_select supports end_year, too.
date_select("user", "birthday", :start_year => 1940, :end_year => Date.current.year - 13)
All dates in the database are stored in UTC and all dates in Ruby are in a local timezone
With the timezone support introduced in Rails 2.1 the idea is that all dates in the database are stored in UTC and all dates in Ruby are in a local timezone.
Time.zone.now == Time.now # => false
as Peter Marklund lights up this in his blog:
http://marklunds.com/articles/one/402
“They will only be converted to UTC for you if they are ActiveSupport::TimeWithZone objects, not if they are Time objects. This means that you are fine if you use Time.zone.now, 1.days.ago, or Time.parse(”2008-12-23“).utc, but not if you use Time.now or Time.parse(”2008-12-23“)”
Passing html options (Ruby hash parameters)
When you have two default hash parameters at the end of a function call, you need to use it as the following:
options = { :include_blank => true, :default => @my_object.my_method } date_select :my_object, :my_method, options, :class => 'my_css_class'
You can try it for yourself on this example:
def test_funct a = {}, b = {} puts "a: #{a.inspect}" puts "b: #{b.inspect}" end test_funct :x => 'x', :y => 'y' # all the parameters are collected for the hash a
Parsing the Date Params into a New Date Object.
Useful for when you need to create a Date object from these manually, such as for reporting.
If the date select input is start_date and it belongs to the report form object:
@start_date = Date.civil(params[:report]["start_date(1i)"].to_i, params[:report]["start_date(2i)"].to_i, params[:report]["start_date(3i)"].to_i) @start_date.class # => Date @start_date # => Sun, 25 Sep 2011 # For example.
Use a similar method for DateTime situations.
Incorrectly named option
The :add_month_number option should be :add_month_numbers
rails date_select examples
I have combined some of the tips here: http://www.rubyplus.net/2016/11/rails-dateselect-examples.html
Locale
To change default locale by the parameter you can set :locale option, like below:
select_date 'user', 'birth', :locale => 'de'
Default field order
If you want to set an app wide default order for the fields (rather than passing :order each time), use the locale file.
eg. edit config/locale/en.yml to include:
en: date: order: - :day - :month - :year
:discard_month implicitly sets :discard_day to true
This may not be the behaviour that you want, and setting :discard_day => false doesn’t change this. One way of getting around this is to hide the month field using CSS e.g.
#some_date_field_2i { display:none; }
If you use the :default option for the date_select, the correct default month will be passed through to the controller. Using this with :discard_year will give you a dropdown with only the day, but preserve the month and year as provided by :default.