Search code examples
htmlruby-on-railsruby-on-rails-3form-helpers

How to work with date_select form helper without a model in Rails?


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 %>

Solution

  • 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.