Rails 3:将时区设置为当前用户时区 [英] Rails 3: setting the timezone to the current users timezone
本文介绍了Rails 3:将时区设置为当前用户时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我把它放在应用程序控制器中:
I put this in Application Controller:
before_filter :set_timezone
def set_timezone
Time.zone = current_user.time_zone
end
但我总是收到错误:
undefined method time_zone for #<User:0xa46e358>
我就是不知道为什么...
and I just don't know why...
希望有人能帮忙
推荐答案
Max -- the ryandaigle.com 文章 您提到了 这篇文章,您需要在其中创建迁移以将time_zone"作为属性添加到用户
Max -- the ryandaigle.com article you mentioned links to this writeup where you need to create a migration to add "time_zone" as an attribute to the user
(本文来自文章,采用 rails 2.x 语法)
(this is from the article, in rails 2.x syntax)
$ script/generate scaffold User name:string time_zone:string
$ rake db:migrate
稍后
<%= f.time_zone_select :time_zone, TimeZone.us_zones %>
这就是您的 .time_zone 返回 method_missing 的原因——您还没有将 time_zone 存储在用户身上.
That's why your .time_zone is returning a method_missing -- you haven't stored the time_zone on the user yet.
这篇关于Rails 3:将时区设置为当前用户时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文