Is there a way to get date_select to "select" another date besides "today"?

Is there a way to use date_select in the form_for helper and have the displayed date in the drop-down list be something besides "today"? I'm trying to get it to be "yesterday" without success. I also tried

<% d = Date.today %> <% d = d-1 %> <%= select_date (date=d) %>

and that didn't work either.

Thanks.

Sharon

Sharon,

I assume you're trying to have a date field default to yesterday in the form if it hasn't already been set by the user? Try setting it to yesterday in the controller. So if you have an ActiveRecord object called event that includes an attribute called scheduled_date, do something like this in the controller action:

event.scheduled_date ||= 1.day.ago

Now you should be able to use the form_for/date_select and get the right result. Alternatively, you could use the select_date helper... should look something like:

select_date(1.day.ago)

Cheers, Ken

Sharon Machlis wrote:

Is there a way to use date_select in the form_for helper and have the displayed date in the drop-down list be something besides "today"? I'm trying to get it to be "yesterday" without success. I also tried

<% d = Date.today %> <% d = d-1 %> <%= select_date (date=d) %>

and that didn't work either.

From api.rubyonrails.org you should use

select_date(d)

The notation select_date(date = Date.today, options = {}) means - if there is no parameter supplied it uses Date.today as first parameter and an empty hash as options.

Stefan