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

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:

  date_select("post", "written_on")
  date_select("post", "written_on", :start_year => 1995)
  date_select("post", "written_on", :start_year => 1995, :use_month_numbers => true,
                                    :discard_day => true, :include_blank => true)
  date_select("post", "written_on", :order => [:day, :month, :year])
  date_select("user", "birthday",   :order => [:month, :day])

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

Show source
Register or log in to add new notes.
August 28, 2008
9 thanks

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
August 25, 2008 - (v2.1.0)
4 thanks

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)
February 17, 2009
4 thanks

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)'
)
August 13, 2008 - (v2.1.0)
2 thanks

end_year

date_select supports end_year, too.

date_select("user", "birthday", :start_year => 1940, :end_year => Date.current.year - 13)
January 29, 2010
1 thank

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
September 25, 2011
1 thank

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.

February 4, 2010 - (>= v2.1.0)
0 thanks

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“)”

December 23, 2008
0 thanks

Incorrectly named option

The :add_month_number option should be :add_month_numbers

March 1, 2013
0 thanks

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
April 19, 2012 - (v2.3.2 - v3.2.1)
0 thanks

Locale

To change default locale by the parameter you can set :locale option, like below:

select_date 'user', 'birth', :locale => 'de'
June 16, 2009
0 thanks

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