Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

helpers - where to put time format rules in Rails 3?

I am finding myself repeating typing many strftime which I defined.

Having watch Ryan Bates's railscasts ep 32/33( I think), I created a custom option for the to_s method as in Time.now.to_s, so that I can do Time.now.to_s(:sw), where :sw is my custom method, to retrieve "23 Sep 2010, 5:00PM" for example.

But the problem is, I don't know where to put #sw's definition. Should it be in a file in in the initializer folder? Or should it go in application.rb?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use "time" instead of "date" in your locales file, since Rails timestamps are datetimes.

in config/locales/en.yml

en:
  time:
    formats:
      default: "%Y/%m/%d"
      short: "%b %d"
      long: "%B %d, %Y"

in app/views/posts/show.html.haml

  = l post.updated_at
  = l post.created_at, :format => :long

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...