我的事件控制器中有一个快速添加操作,因为客户端实际上只在给定日期的三个不同时间段安排事件。日期和时间在默认形式下工作正常,但尝试手动设置值给我带来了一些麻烦。
def quick_add #params are date like 2012-04-29, timeslot is a string
timeslot = params[:timeslot].to_sym
date = params[:date].to_date
@workout = Workout.new do |w|
w.name = 'Workout!'
w.date = date
case timeslot
when :morning
w.time = Time.local(w.date.year, w.date.month, w.date.day, 6)
when :noon
w.time = Time.local(w.date.year, w.date.month, w.date.day, 12)
when :evening
w.time = Time.local(w.date.year, w.date.month, w.date.day, 18, 15)
else
w.time = Time.now
end
end
正在创建事件,日期正确,但时间是:
Morning: 2000-01-01 10:00:00 UTC
Expected: 2012-05-02 06:00:00 UTC -400
Noon: 2000-01-01 16:00:00 UTC
Expected: 2012-05-02 12:00:00 UTC -400
Evening: 2000-01-01 22:15:00 UTC
Expected: 2012-05-02 18:15:00 UTC -400
值得注意的是,在 rails 控制台中运行命令似乎得到了我期望的结果。