我有一个Rails API应用程序,我正在寻找一种方法来确保我的所有序列化程序以一种特定的方式格式化他们的to_json时间数据。我不想通过代码编写created_at.to_s(:db),但我似乎找不到绕过它的方法。
发布于 2016-01-08 22:20:51
所以我在猴子贴片中找到了解决方案:
将下面的内容添加到initializers/date_time.rb中可以很好地完成这个任务。
ActiveSupport.use_standard_json_time_format = false
class DateTime
def as_json(options = nil) #:nodoc:
if ActiveSupport.use_standard_json_time_format
strftime("%FT%T.%3NZ")
else
strftime("%F %T")
end
end
end
class ActiveSupport::TimeWithZone
def as_json(options = {})
strftime('%F %T')
end
endhttps://stackoverflow.com/questions/34685006
复制相似问题