如何在没有Rails模型的情况下使用date_select表单助手?

时间:2011-09-22 23:47:31

标签: html ruby-on-rails ruby-on-rails-3 form-helpers

我有一个变量@the_date和一个没有关联模型的date_select表单助手。

如何使用date_select显示相应的HTML?

以下不起作用:

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

2 个答案:

答案 0 :(得分:3)

你可以这样做:

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

答案 1 :(得分:1)

这是最终奏效的内容:

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

我将日期存储为字符串。因此,它需要在显示在HTML表单之前转换为日期对象,并在保存到数据库之前转换回字符串。

相关问题