简体   繁体   English

如果数据库中为零,Rails DateTime字段将具有当前日期/时间

[英]Rails DateTime field has current date/time if nil in the database

I have a form with a datetime field. 我有一个带有datetime字段的表单。 Since Rails 3.2.13 doesn't support type="datetime-local" input fields yet (and I'm not ready to move to 4.0 beta), I'm doing this manually: 由于Rails 3.2.13尚不支持type="datetime-local" input字段(并且我还不准备移至4.0 beta),因此我将手动执行此操作:

<input type="datetime-local" name="person[my_date_time]"<% if @person.my_date_time %> value="<%=@person.my_date_time.strftime('%FT%T')%>"<% end %>/>

The problem is @person.my_date_time will be the current date/time if it's nil in the database. 问题是@ person.my_date_time将是当前日期/时间(如果在数据库中nil And it outputs with seconds too which screws up Chrome's handling of datetimes. 而且它的输出也以秒为单位,这加深了Chrome对日期时间的处理。 I want that field to be empty if it's nil in the database. 我想那场是空的,如果它是nil到数据库中。 How can I tell? 我怎么知道? Rails' datetime_select method handles it the way I want. Rails的datetime_select方法以我想要的方式处理它。

If the datetime field is nil in the database something in your application must be changing or overwriting that value. 如果数据库中的datetime字段为nil ,则您的应用程序中的某些内容必须正在更改或覆盖该值。 Perhaps you have a default somewhere? 也许您在某处有默认值? It's not really possible to say what is going on without seeing more code than what is posted now. 在没有看到比现在发布的更多的代码的情况下,真的不可能说出正在发生的事情。 Can you post the code for you Person model and any other relevant code (any gems which might affect things, etc.)? 可以为您的Person模型发布代码以及任何其他相关代码(任何可能影响事物的宝石等)吗?

strftime("%FT%R")  # Will output without seconds, e.g. "2013-04-18T21:43"

http://www.ruby-doc.org/core-1.9.3/Time.html#method-i-strftime http://www.ruby-doc.org/core-1.9.3/Time.html#method-i-strftime

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

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