如何使用 MySQL 5.1 更改 Rails 2.0.2 中的默认类型日期格式

发布于 2024-10-12 10:47:30 字数 736 浏览 2 评论 0原文

我正在寻找一种可以将默认日期格式转换为特定用户格式的方法。

我正在开发 Rails 2.0.2。正如我们所知,在使用 Scaffold 命令时,我们会自动获取“id”、“created_at”等属性。 “updated_at”作为表的一部分。

我的脚手架命令如下所示:-

script/server scaffold posts name:string title:string content:text

我基本上是在尝试实现一个博客应用程序。现在,当我尝试检查发布特定博客的日期时,我使用帮助标记并通过 <%=h post.created_at %>我的index.html.erb 文件..

我能够显示博客最初以默认格式创建的日期,目前的格式如下:Tue Jan 18 13:00:05 +0530 2011 via MySql 5.1数据库。我想将此格式更改为月日年..就像在上面的情况下,它将是 2011 年 1 月 18 日。

在这方面,您能告诉我该怎么做吗?我不确定我需要在哪里进行哪些更改。

有没有一种方法可以将数据存储在index.html.erb 中并将其转换为用户定义的格式?我不太确定如何直接从视图中进行处理。

另外,我想如果硬编码的话,这将是一种 DRY(不要重复自己)行为,违反了 Rails 原则。您能建议一个合适的方法吗?我可以根据最终用户的要求进行更改。

谢谢..

I am looking for a way through which I could convert my default date format to a specific user format.

I am working on Rails 2.0.2. As we know on making use of the Scaffold command we automatically get the attributes of "id", "created_at" & "updated_at" as part of your table.

My scaffold command looks like this:-

script/server scaffold posts name:string title:string content:text

I am basically trying to implement a blog application. Now when I try to check the date when a particular blog is posted, I make use of the helper tags and through <%=h post.created_at %>in my index.html.erb file..

I am able to display the date a blog was originally created in default format which is currently like: Tue Jan 18 13:00:05 +0530 2011 via MySql 5.1 DB . I want to change this format to Month date Year.. like in the above case it would be January 18 2011.

In this regard could you please tell me how do I go about it. I am unsure of where do I need to make what changes.

Is there a way through which I can store the data in the index.html.erb and convert it then and there to the user defined format? I am not too sure of how to go about it directly from the view..

Also, I guess this, if hard coded would be a DRY(Don't Repeat Yourself) violation going against Rails principles. Could you suggest an appropriate way. Something that I can change as per a end user requirement.

Thank you..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜你已久 2024-10-19 10:47:30

您可以在视图中使用 date_select_tag 并将其传递到时间对象中,它将为您带来神奇的效果。比方说,你的时间对象是在那时创建的,

<%= date_select_tag, :created_at, @user.created_at %>

Rails 会负责来回转换它。

编辑:
此外,您可以使用 @user.created_at.to_date.strftime() 将其转换为视图的日期,如此处所述 - http://www.ruby-doc.org/core/classes/Time.html#M000392

You could use date_select_tag in the view and pass it in the time object and it would do the magic for you. Lets say for example, your time object is created at then,

<%= date_select_tag, :created_at, @user.created_at %>

Rails would take care of converting it back and forth..

Edit:
Also, you can convert it into date for views using @user.created_at.to_date.strftime() as documented here - http://www.ruby-doc.org/core/classes/Time.html#M000392

甜点 2024-10-19 10:47:30

Rails 处理日期和时间转换格式的方式与您使用的数据库无关。要覆盖默认格式,您应该向应用程序添加一个初始值设定项。我的在 config/initializers/time_formats.rb 中。然后在该文件中,您将定义要在整个应用程序中使用的格式:

Time::DATE_FORMATS[:just_date] = "%b %d, %Y"
Time::DATE_FORMATS[:just_time] = "%I:%M%p"

为了使用新格式,您将把符号传递给字符串转换,如下所示:

<%= @post.created_at.to_s(:just_date) %>

The way Rails handles date and time conversion formats is independent of which database you are using. To override the default format, you should add an initializer to your application. I have mine at config/initializers/time_formats.rb. Then in that file you would define the formats you want to use throughout the app:

Time::DATE_FORMATS[:just_date] = "%b %d, %Y"
Time::DATE_FORMATS[:just_time] = "%I:%M%p"

In order to use the new formats, you will pass the symbol to a string conversion like so:

<%= @post.created_at.to_s(:just_date) %>
守护在此方 2024-10-19 10:47:30

您可以使用例如
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS

您可以合并到您自己的(在environment.rb中)例如
ActiveSupport::CoreExtensions::时间::转换::DATE_FORMATS.merge!(
:默认=> '%m/%d/%Y',
:date_time12 => “%m/%d/%Y %I:%M%p”,
:date_time24 => “%m/%d/%Y %H:%M”

You can use for example
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS

You can merge in your own (in environment.rb) for example
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
:default => '%m/%d/%Y',
:date_time12 => "%m/%d/%Y %I:%M%p",
:date_time24 => "%m/%d/%Y %H:%M"
)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文