[英]Hidden date field setting attributes to nil in rails console (but not in the database)
[英]Rails DateTime field has current date/time if nil in the database
我有一個帶有datetime
字段的表單。 由於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 %>/>
問題是@ person.my_date_time將是當前日期/時間(如果在數據庫中nil
。 而且它的輸出也以秒為單位,這加深了Chrome對日期時間的處理。 我想那場是空的,如果它是nil
到數據庫中。 我怎么知道? Rails的datetime_select
方法以我想要的方式處理它。
如果數據庫中的datetime字段為nil
,則您的應用程序中的某些內容必須正在更改或覆蓋該值。 也許您在某處有默認值? 在沒有看到比現在發布的更多的代碼的情況下,真的不可能說出正在發生的事情。 可以為您的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.