Rails表单在当地时区显示datetime [英] Rails Form display datetime at local timezone
问题描述
我不想只是在提交之后更改它,因为这将对用户感到困惑。当您打开新窗体时,默认时间是当前时间(当前UTC)。我希望这是当地时区的时间。
<%= f.label:Date_Time%>< br />
<%= f.datetime_select:date%>
谢谢,
加载方法来执行此操作!
我建议您查看问题Stepan推荐首先
服务器端
如果您希望保持时间一致,您可能希望使用Rails内置时区选项:
#config / application.rb
config.time_zone ='Eastern Time(US& Canada)'
您可以查看有关 ActiveSupport :: Timezone 这里
客户端
客户端,您必须考虑想要实现的目标。如果您已经设置了时区,您可以从创建TimeZone存储库( ENV
变量或datatable)中受益,并允许用户从中选择,如下所示:
#app / views / form.html.erb
<%= form_tag%>
<%= select_tag:date,options_from_collection_for_select(@timezones,id,name)%>
<%end%>
或者,您可以通过javascript传递时区:使用JavaScript获取客户端的时区
UX
最重要的是,您需要考虑到要实现的目标。如果您的用户需要本地化时间,您应该使用一个国家/地区选择系统,他们将在何处为您提供国家/地区,并且您可以投放时区
从经验来看,更好地平均存储所有时间(使用 UTC
),然后使用用户的规格处理它们
I'm new to rails (just finished the Rails Tutorial) and I'm building my first solo app. The user can create 'events' at specific times. Currently the user form is in UTC. How do I set it to their local time?
I don't just want to change it after they submit, because it would be confusing for the user. When you open a new form the default time is the current time (currently UTC). I want that to be the time of their local timezone.
<%= f.label :Date_Time %><br />
<%= f.datetime_select :date %>
thanks,
Loads of ways to do this!
I'd recommend you look at the question Stepan recommended first
Server Side
If you're wanting to keep your times consistent, you may wish to use the Rails in-built timezone option:
#config/application.rb
config.time_zone = 'Eastern Time (US & Canada)'
You can see more about ActiveSupport::Timezone here
Client-Side
Client-side, you'd have to think about what you want to achieve. If you have set timezones, you may benefit from creating a TimeZone repository (either ENV
variables or datatable), and allow user to select from them, like this:
#app/views/form.html.erb
<%= form_tag %>
<%= select_tag :date, options_from_collection_for_select(@timezones, "id", "name") %>
<% end %>
Alternatively, you could pass the timezone through javascript: Getting the client's timezone in JavaScript
UX
The bottom line is you need to think about what you're trying to achieve. If your users need localized times, you should use a country-select system, where they will give you their country, and you can dedicate a timezone
Speaking from experience, it's much better to store all times equally (using UTC
), and then process them using user's specifications
这篇关于Rails表单在当地时区显示datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!