valibuk.net

A place for opinions and notes of a valibuk.

How To Use date_select for Birth Date Field

Tagged with: — ondrej at 11:08 pm on Tuesday, July 18, 2006

Ruby on Rails The date selection date_select widget by default contains years from five years before to five years after the current date.

This is not sufficient for a birth date field.

Default Data Selection

Let’s assume that you used the scaffold generator to generate controller and views; a date field should look like:

  1. <p><label for="employee_date_of_birth">Date of birth</label><br/>
  2. <%= date_select ‘employee’, ‘date_of_birth’ %></p>

This date selection is not sufficient (maybe it is if your application is designed for a kindergarten ;).

Set Options

We have to set the start_year and the end_year options.

The start_year should contain a value equal to the current year and decreased by the supposed maximal age; the end_year should contain a value equal to the current year. The current year we can get by calling the following code:

Time.now.year

Solution

A date selection with a correct list of years:

  1. <p><label for="employee_date_of_birth">Date of birth</label><br/>
  2. <%= date_select ‘employee’, ‘date_of_birth’,
  3.     :start_year => Time.now.year - 100,
  4.     :end_year => Time.now.year %></p>

Advanced Solution

If you plan to use this on more places, of course you should reduce the amount of a copy&paste code and if there is a requirement to change the boundaries, it should be in one place.

Add the following code to the app/helpers/application_helper.rb file the following lines:

  1. def getBirthDateStart()
  2.   Time.now.year - 100
  3. end
  4.  
  5. def getBirthDateEnd()
  6.   Time.now.year
  7. end

and change the date selection code to:

  1. <p><label for="employee_date_of_birth">Date of birth</label><br/>
  2. <%= date_select ‘employee’, ‘date_of_birth’,
  3.     :start_year => getBirthDateStart(),
  4.     :end_year => getBirthDateEnd() %></p>

That is it :)

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • DZone
  • Digg
  • Reddit
  • Technorati
  • Furl
  • NewsVine
  • Slashdot
  • Ma.gnolia
  • StumbleUpon

4 Comments »

3

Comment by Aníbal Rojas

July 19, 2006 @ 3:43 am

Off Topic: This is just a quick note to invite you to register your blog at RubyCorner.com, a meeting place for people interested in the Ruby Programming Language or any of the related technologies.

4

Comment by ondrej

July 19, 2006 @ 8:43 am

Thanks :)

5

Comment by Brandon Keepers

July 19, 2006 @ 2:42 pm

Since you’re adding helpers to eliminate duplication, why no save yourself even more typing?


def birthdate_select(object, method, options = {})
date_select object, method, {:start_year => Time.now.year - 100,
:end_year => Time.now.year}.merge(options)
end


Date of birth

6

Comment by ondrej

July 19, 2006 @ 2:51 pm

You are right :)

I will only add an example how to use it:

< %= birthdate_select ‘employee’, ‘date_of_birth’ %>

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comment Preview


commercial break :)

Make an account on slicehost.com -- a really good hosting where you have your own virtual machine. I installed Gentoo there = I like it very much ;)