如何更改ActiveAdmin如何显示时间(每次) [英] How to change how ActiveAdmin displays time (every time)

查看:157
本文介绍了如何更改ActiveAdmin如何显示时间(每次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于数据库中的默认时间在utc中,我希望能够在用户正确的时间内显示它。为此,我不得不采取列:created_at 并将其更改为:

Since the default time in the database is in utc, I wanted to be able to still display it in the users correct time. To do this I had to take column :created_at and change it into this:

index do
...
  column :created_at, :sortable => :created_at do |obj|
    obj.created_at.localtime.strftime("%B %d, %Y %H:%M)
  end
...
end

似乎很容易做一两次,但是当你需要覆盖每个索引和显示方法时,这个过程就会有点税收。

Seems pretty easy to do once or twice, but when you need to override every index and show method, the process get's a little taxing.

有没有办法来覆盖ActiveAdmin如何显示时间,而不必重写每次发生?

Is there a way to override how ActiveAdmin displays time without having to override each occurrence?

我知道我可以创建一个函数或者可能使用提供的时间更好,但是我每次想要显示时间时都必须使用它,我想覆盖它,而不用担心我错过了一个。

I know I could create a function or probably use the functions provided for time better, but I'd still have to use it each time I want to display time. I want to override it without worrying I missed one.

推荐答案

我发现有两种方法可以做到这一点:

I've found there are two ways to do this:

这是一个更简单的方法,因为它非常简短,不涉及添加数据库字段。它的工作原理是因为它的浏览器使用了时区。这个答案给出了解如何做到这一点。

This is the easier method because it is very short and doesn't involve adding a db field. It works because it uses the users timezone from their browser. This answer gives insight on how to do this.

2。从数据库

此过程可以在此轨道

在users表中基本存储一个time_zone字段。然后使用before_filter(尽管around_filter可能是一个更好的主意)来设置控制器中的时区。

Essentially store a time_zone field in the users table. Then use an before_filter (although an around_filter might be a better idea) to set the timezone in the controller.

before_filter :set_time_zone

private
def set_time_zone
  Time.zone = current_user.time_zone if current_user
end

这篇关于如何更改ActiveAdmin如何显示时间(每次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆