开发者

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

开发者 https://www.devze.com 2023-04-07 12:05 出处:网络
I have a variable @the_date and a date_select form helper without an associat开发者_如何学Goed model.

I have a variable @the_date and a date_select form helper without an associat开发者_如何学Goed model.

How to use date_select to display the appropriate HTML?

The following does not work:

<%= date_select "the_date", @the_date %>


You can do:

<% @the_date = Time.now %>
<%= date_select("my_date", "my_date", :default => @the_date) %>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消