Rails time_ago_in_words producing bad output

别等时光非礼了梦想. 提交于 2020-01-02 20:13:33

问题


I thought this might be due to moving to activesupport 2.3.5 but now I believe something else must have happened.

Model has a valid rfc822 style date:

>> s.lastVisitDate
=> "Thu, 06 Jan 2011 22:24:10 -0800"

But in my view:

<%=h time_ago_in_words(@site.lastVisitDate) -%>

renders: *about {{count}} hours ago* 
instead of: *about 2 hours ago* which was working just fine earlier.

Wondering if anyone else has seen this behavior. I've reviewed my version history for the model and view and nothing has changed recently so I believe I must have messed up something on the config side of things.


回答1:


I found that I was missing the appropriate values in a locale file.

So in my case I added the following to /config/locales/en.yml

I am not sure why this was previously working or what specific gem or config change triggered this issue but having the proper definition here make actionpack happy.

# Used in distance_of_time_in_words(), distance_of_time_in_words_to_now(), time_ago_in_words()
  datetime:
    distance_in_words:
      half_a_minute: "half a minute"
      less_than_x_seconds:
        one:   "less than 1 second"
        other: "less than %{count} seconds"
      x_seconds:
        one:   "1 second"
        other: "%{count} seconds"
      less_than_x_minutes:
        one:   "less than a minute"
        other: "less than %{count} minutes"
      x_minutes:
        one:   "1 minute"
        other: "%{count} minutes"
      about_x_hours:
        one:   "about 1 hour"
        other: "about %{count} hours"
      x_days:
        one:   "1 day"
        other: "%{count} days"
      about_x_months:
        one:   "about 1 month"
        other: "about %{count} months"
      x_months:
        one:   "1 month"
        other: "%{count} months"
      about_x_years:
        one:   "about 1 year"
        other: "about %{count} years"
      over_x_years:
        one:   "over 1 year"
        other: "over %{count} years"
      almost_x_years:
        one:   "almost 1 year"
        other: "almost %{count} years"
    prompts:
      year:   "Year"
      month:  "Month"
      day:    "Day"
      hour:   "Hour"
      minute: "Minute"
      second: "Seconds"


来源:https://stackoverflow.com/questions/4623762/rails-time-ago-in-words-producing-bad-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!