You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用simple_form设置事件时间时区显示错误,求解决方法

Fixing Timezone Issues with Simple_form Datetime Inputs

It looks like the core issue here is that Simple Form isn't respecting your Rails application's configured timezone when rendering datetime inputs. Let's break down the solution step by step:

1. Double-Check Your Rails Timezone Configuration

First, make sure your app is properly set to use your desired timezone. In config/application.rb, confirm you have:

config.time_zone = 'Moscow' # Or use 'Europe/Moscow' for daylight savings support
config.active_record.default_timezone = :utc # Keep this as UTC (best practice for database storage)

You mentioned Time.zone.now works correctly in the console, so this part is likely already set up right—this just ensures we're on the same page.

2. Configure Simple Form to Use Your App's Timezone

Simple Form doesn't automatically inherit the Rails timezone for datetime inputs by default. You have two options to fix this:

Option A: Global Configuration (Apply to All Datetime Inputs)

Add this to config/initializers/simple_form.rb:

SimpleForm.setup do |config|
  # ... keep your existing config

  # Tell Simple Form to use the app's timezone for datetime inputs
  config.input_options[:datetime] = { time_zone: Time.zone }
end

This will apply the timezone setting to every datetime input across your app.

Option B: Per-Input Configuration (Target Specific Fields)

If you only need to fix this for your event form, modify your Slim view to pass the timezone parameter directly to each datetime input:

= simple_form_for @event do |f|
  = f.input :name
  = f.input :start_time, time_zone: Time.zone
  = f.input :end_time, time_zone: Time.zone
  = f.input :camera_ids, collection: Camera.all, as: :check_boxes
  = f.button :submit, class:'btn-primary'

Why Your Previous .in_time_zone Attempt Didn't Work

When you tried @event.start_time.in_time_zone('Moscow') directly in the input, it didn't work because Simple Form splits datetime inputs into separate fields (like start_time(4i) for hours, start_time(5i) for minutes) instead of using a single value attribute. The time_zone parameter tells Simple Form how to convert the UTC-stored time from your database into your local timezone when rendering these split fields.

How It Works Under the Hood

  • Your database stores times in UTC (which is correct for consistency)
  • Rails automatically converts these UTC times to your app's configured timezone when accessing @event.start_time
  • By adding the time_zone: Time.zone parameter, Simple Form uses that timezone to render the split datetime fields correctly
  • When you submit the form, Simple Form will convert the user-inputted local time back to UTC before saving to the database

After applying either of these fixes, your start and end times should display correctly in your +03:00 timezone instead of showing UTC times as local.

内容的提问来源于stack exchange,提问作者Ngoral

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:52:56