Question

In Rails Admin, when editing a record, datetime values are always displayed in UTC.

When I set the value it is recognized in my current timezone setting (PST). Here's an image of the value that I set it to within the edit view before saving:

Setting value to 8pm

After saving, the value is displayed in UTC:

Value displayed as 12pm

Is there any way to display these values in a specific timezone, so that administration of datetime attributes in RailsAdmin is less confusing for the user?

Was it helpful?

Solution

The following gist, in addition to setting a default timezone, resolved my issue. PST Datetimes are now displayed in RailsAdmin views. I had to precompile assets to get it working on my heroku environments:

# rails_admin.rb
# https://gist.github.com/jackdesert/7090731

require 'rails_admin/config/fields/base'

module RailsAdmin
  module Config
    module Fields
      module Types
        class Datetime < RailsAdmin::Config::Fields::Base
          def value
            value_in_default_time_zone = bindings[:object].send(name)
            return nil if value_in_default_time_zone.nil?
            pacific_time_zone = ActiveSupport::TimeZone.new('Pacific Time (US & Canada)')
            value_in_default_time_zone.in_time_zone(pacific_time_zone)
          end
        end
      end
    end
  end
end

In staging.rb and production.rb I specified the PST timezone:

config.time_zone = 'Pacific Time (US & Canada)'
config.active_record.default_timezone = 'Pacific Time (US & Canada)'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top