2013-09-16 31 views
1

我已閱讀了很多關於此問題的帖子,但沒有一篇似乎明確地解決它。在表單信息上顯示信息時不會發生時區轉換

基本上我有一個時間字段doc_date

當我保存日期,我把它保存在我的本地區域(里斯本,目前與DST:GMT +1)的模型Receipt。 在DB中我有Utc(gmt)日期。也就是說,比我目前所在的時間少一小時。

到目前爲止,這麼好。

問題是當我將我的領域加載到窗體。

<%= form_for(@receipt) do |f| %> 
[...] 
<div class="field"> 
     Date Test </br> 
     <%= @receipt.doc_date %> 
    </div> 
<div class="field"> 
    <%= f.label :doc_date %> 
    <%= f.text_field :doc_date %> 
</div> 
[...] 
<% end %> 

如果保存日期是比方說,「2013年8月1日,00:00」,在文件日期輸入字段顯示的日期是「2013 23:00 7月31日,」。在另一端

日期試驗證明是正確的

Date Test 
2013/08/01 00:00 

在我application_controller我也有

before_filter :set_user_time_zone 

    #Sets the user default time zone to the one configured in the db 
    def set_user_time_zone 

    Time.zone = current_user.time_zone if user_signed_in? 
    end 

在我的application.rb中我有config.time_zone = 'Lisbon'

我跑的Rails 3.2。

如果我運行Receipt.where(ID:18365)在控制檯.first.doc_date,日期也返回00:00 8月1日,所以這個問題似乎是加載的形式...

所以這似乎是要麼Rails跳過表單字段的時區轉換,或與事實相關的事實,我有該字段的jQuery的日期選擇日曆...

任何建議,以顯示正確的本地日期時間在表單字段?

+1

這個問題可以幫助你 - http://stackoverflow.com/questions/6554075/rails -config-time-zone-doesnt-apply-to-datetime-data-loaded-into-a-text-field – trh

+0

絕對如此。增加值:f.object.doc_date,做了訣竅。 '<%= f.text_field:doc_date,value:f.object.doc_date%>'。謝謝 ! – MrWater

+0

@trh想寫這個答案? – MrWater

回答