How to set default month or day in f.date_select field

hi all,

i have got a problem in my project. i have f.date_select field in my
form and i want to set the default date = current date+30. but
f.date_select is not allowing me to do so. there are options to set year
only not month and day.
see what i have followed:

<% date_object = Time.now %>
<% date_object = date_object.to_time.advance(:months => 1).to_date %>

<%= f.date_select :valid_til, :default => {:year => date_object.year,
:month => date_object.month}, :order => [:month, :day, :year ],
:tabindex => “3” %>

-------but this will work only for year not for month and day------

<%= f.date_select :valid_til, :start_year => date_object.year,
:start_month => date_object.month, :start_day => date_object.day :order
=> [:month, :day, :year ], :tabindex => “3” %>

-------but this also will work only for year not for month and day------

if anyone is known to this problem and solution please help me out from
this. your help will be appreciated.

thanks

Can you set it in the controller?

For example, if the form is for a new entry:

@new_record = Something.new
@new_record.valid_til = date_object

What would you do if this is for an existing record?

I have nearly the same issue, but the problem for me is that the
existing record is currenty null, and the date_select seems to be
defaulting to today for its date.

Dustin

On Jun 4, 5:05 pm, Phillip K. [email protected]

Tubahead wrote:

What would you do if this is for an existing record?

I have nearly the same issue, but the problem for me is that the
existing record is currenty null, and the date_select seems to be
defaulting to today for its date.

Dustin

On Jun 4, 5:05�pm, Phillip K. [email protected]

Hello Dustin,

You can do two things. If you must have a date in the form, then do the
same thing for the existing record as you do for the new one: set it in
the controller. Otherwise, if the date can actually be blank, add
:include_blank => true to the f.date_select statement.

Keep in mind that when using form_for, and therefore the f.* notation,
FormBuilder will associate the date selector with the valid_til method
on whatever object you pass into form_for. When initialized, whatever
value you have in object.valid_til will be used to set the date
selector’s date. If valid_til is nil, the current date will be used,
unless :include_blank is true, and then it will be blank.

Peace,
Phillip

Denise R. wrote:

Can you set it in the controller?

For example, if the form is for a new entry:

@new_record = Something.new
@new_record.valid_til = date_object

If this for new records, which it sure does sound like, Denise’s idea is
spot on. Just set it in the object when you create it and don’t futz
with the rest.

Peace,
Phillip

Thanks !!

It worked great - I am quickly becoming a rails convert (this would
have really sucked in php).

This was what finally did what I sort of expected:

Date of Birth: <%= f.date_select :customers_dob, :start_year => 1920, :use_month_numbers => true, :include_blank => true %>

Dustin
On Jun 15, 7:02Â am, Phillip K. [email protected]