简体   繁体   English

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

[英]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. 我有一个变量@the_date和一个date_select表单帮助器,没有关联的模型。

How to use date_select to display the appropriate HTML? 如何使用date_select显示适当的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. 因此,需要先将其转换为日期对象,然后再以HTML形式显示,然后再转换回字符串,然后再保存到数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM