【发布时间】:2023-03-05 07:06:01
【问题描述】:
我有一个变量 @the_date 和一个没有关联模型的 date_select 表单助手。
如何使用date_select显示相应的HTML?
以下不起作用:
<%= date_select "the_date", @the_date %>
【问题讨论】:
标签: html ruby-on-rails ruby-on-rails-3 form-helpers
我有一个变量 @the_date 和一个没有关联模型的 date_select 表单助手。
如何使用date_select显示相应的HTML?
以下不起作用:
<%= date_select "the_date", @the_date %>
【问题讨论】:
标签: html ruby-on-rails ruby-on-rails-3 form-helpers
你可以这样做:
<% @the_date = Time.now %>
<%= date_select("my_date", "my_date", :default => @the_date) %>
【讨论】:
<%= date_select("the_date", "", :default => @the_date ) %>
这就是最终奏效的方法:
<% @the_date = Date.strptime(@the_date_string, "%Y-%m-%d") %>
<%= date_select("the_date_string", "", :default => @the_date) %>
我将日期存储为字符串。因此,需要将其转换为日期对象才能显示在 HTML 表单中,并转换回字符串才能保存到数据库中。
【讨论】: