Changing the timestamp of magic columns (Rails)
I want to change the default timestamp of the magic columns of rails (created_at, modified_at) instead of 2010-09-03 14:52:46 UTC I'd like to change it to September 10, 2010 at 02:52 PM. or at least parse that way.
Now, i know that i can do this right in the view, by manipulating the variable, but isnt there a more "railish way" where i can just do this in the mod开发者_如何学编程el, and then call it as a method?
class YourModel < ActiveRecord::Base
def my_formated_date_time
created_at.strftime("%B %d, %Y at %I:%M %p")
end
end
Or you can use the localization format. Add this to your config/locale/your_locale.yml
. IMHO: This would be the most railish way.
your_locale:
time:
formats:
default: "%B %d, %Y at %I:%M %p"
Then in your view do this
<%=l @your_model.created_at %>
<!-- short for: -->
<%= I18n.localize @your_model.created_at %>
Modifying the created_at
date is fine, it will stay as you set it, and you can over-ride it even at the start, but updated_at
or modified_at
might prove tricky. Every time you save your model this is changed. If you don't want this behavior, convention suggests switching to a different name.
If you want to adjust how dates are displayed, you need to configure the default formatter.
精彩评论