Forum: Ruby on Rails How do I set the default value for date fields?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A6b74fef495a52a7d868534e10024091?d=identicon&s=25 Sean Clark (smc7000)
on 2006-01-04 18:39
I want my date fields to default to NIL or "", because the presence of a
date means something.  My problem is that rails is defaulting the fields
to today's date automatically when I create a new record through the
scaffold.  I tried removing the default '0000-00-00' and changing NOT
NULL to NULL, but both columns get today's date as the default  (see
below for the table's sql syntax).

So it looks like I need to code something, but I'm unsure to put that in
the model or in the view (or if this is a form helper deal).  Any help
is appreciated.  Thank you in advance!

CREATE TABLE volunteers ( ......other columns....
  birthdate date default NULL,
  competency_in_service_on date NOT NULL default '0000-00-00'
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
33478bbfbd0e2bf2737eca60a555af76?d=identicon&s=25 Frank (Guest)
on 2006-01-04 18:56
(Received via mailing list)
I have not tried this,
but Chapter 17 (page 361) of Agile Web Development states you can add a
:include_blank option to the date_select

HTH

Frank
----- Original Message -----
From: "Sean Clark" <smc7000@gmail.com>
To: <rails@lists.rubyonrails.org>
Sent: Wednesday, January 04, 2006 12:39 PM
Subject: [Rails] How do I set the default value for date fields?
A6b74fef495a52a7d868534e10024091?d=identicon&s=25 Sean Clark (smc7000)
on 2006-01-04 19:06
Thanks Frank,
I was on 371 researching partials.  I'll back track and see if that
works.
A6b74fef495a52a7d868534e10024091?d=identicon&s=25 Sean Clark (smc7000)
on 2006-01-04 19:17
got it to work.  Below is an example of the code I changed around in
app/views/admin/_form.html  (Admin is the name of my controller).  The
':include_blank => true' is the part I added.  Thanks for your help

<p><label for="volunteer_birthdate">Birthdate</label><br/>
<%= date_select 'volunteer', 'birthdate', :include_blank => true  %></p
33478bbfbd0e2bf2737eca60a555af76?d=identicon&s=25 Frank (Guest)
on 2006-01-04 19:24
(Received via mailing list)
Glad I could help, others help me out all the time.

Frank
----- Original Message -----
From: "Sean Clark" <smc7000@gmail.com>
To: <rails@lists.rubyonrails.org>
Sent: Wednesday, January 04, 2006 1:17 PM
Subject: [Rails] [Solved]: How do I set the default value for date
fields?
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-04 19:54
Frank wrote:
> Glad I could help, others help me out all the time.
>
> Frank
> ----- Original Message -----
> From: "Sean Clark" <smc7000@gmail.com>
> To: <rails@lists.rubyonrails.org>
> Sent: Wednesday, January 04, 2006 1:17 PM
> Subject: [Rails] [Solved]: How do I set the default value for date
> fields?

I think you can also use :prompt=>"Select Birthday" instead of
:include_blank=>true
This topic is locked and can not be replied to.