如何在Rails中正确使用时区

时间:2013-08-28 03:26:07

标签: ruby-on-rails

逻辑:如果没有今天的日志条目,我想显示占位符练习。如果存在今天的日志条目,则即使它们存在,也不要显示占位符练习。

      .log-entries
        - unless exercise.log_entries.for_user(workout.user).blank? || exercise.last_log_date(workout.user) == Time.zone.today.beginning_of_day
          %p Nice job performing this #{timeago exercise.last_log_date(workout.user)}. Try going up in difficulty this time.
          = exercise.last_log_date(workout.user).to_date
          = exercise.last_log_entry(workout.user).created_at.to_date
          %br
          = Time.current.to_date
          %br
          - for log_entry in exercise.placeholder_log_entries!(workout.user)
            = render 'log_entries/form', log_entry: log_entry, exercise: exercise
        - for log_entry in exercise.log_entries.for_today.order("created_at ASC").for_user(workout.user) do
          = render 'log_entries/form', log_entry: log_entry, exercise: exercise
        - if exercise.log_entries.blank?
          %p.hint.not-performed-yet You haven't performed this exercise yet.

      = link_to exercise.render_add_text, user_workout_exercise_log_entries_path(workout, workout.user, exercise), class: "button radius", remote: true, method: :post
是什么让我感到疯狂......是我今天记录了日志条目,但占位符练习仍在显示。所以第一个unless正在传递。

调试后我会得到这样的值:

exercise last log date: 2013-08-26 00:00:00 -0700 
today: 2013-08-27 00:00:00 -0700 

但是那个exer上次记录日期实际上是今天记录的,就在现在!为什么落后一天?

 #<LogEntry id: 226, reps: 8, weight: 55, effort: nil, description: nil, loggable_id: 3, loggable_type: "Exercise", created_at: "2013-08-27 06:00:12", updated_at: "2013-08-28 03:06:56", user_id: 1, rest_after_set: nil, difficulty: nil, distance: nil, duration: nil, measurement: nil, placeholder: false> 

创建于2013-08-27 !!!为什么在视图中渲染26?我正在失去理智。

1 个答案:

答案 0 :(得分:0)

Ruby on Rails中的时区管理通常要求您设置config.time_zone“设置应用程序的默认时区并为Active Record启用时区感知”并使用本地化帮助程序I18n.l或只是l

示例:

I18n.l(Time.now)

l(Time.now)

请查看:http://guides.rubyonrails.org/i18n.html#configure-the-i18n-module