Question

I have a variable @the_date and a date_select form helper without an associated model.

How to use date_select to display the appropriate HTML?

The following does not work:

<%= date_select "the_date", @the_date %>
Was it helpful?

Solution 2

Here's what finally worked:

<% @the_date = Date.strptime(@the_date_string, "%Y-%m-%d") %>
<%= date_select("the_date_string", "", :default => @the_date) %>

I am storing the date as a string. So, it needs to be converted to a date object before displaying in the HTML form, and converted back to a string before saving in the database.

OTHER TIPS

You can do:

<% @the_date = Time.now %>
<%= date_select("my_date", "my_date", :default => @the_date) %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top