简体   繁体   中英

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

I have a form with a datetime field. 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:

<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. And it outputs with seconds too which screws up Chrome's handling of datetimes. I want that field to be empty if it's nil in the database. How can I tell? Rails' datetime_select method handles it the way I want.

If the datetime field is nil in the database something in your application must be changing or overwriting that value. 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.)?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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