加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用time_ago_in_words获取{{count}}

发布时间:2020-12-17 03:10:04 所属栏目:百科 来源:网络整理
导读:我目前在我的应用程序中有这个代码: def comment_poster(comment) if comment.user "posted by #{comment.user.username} #{time_ago_in_words(comment.created_at)} ago" else "posted by anonymous" end end 但是,这只适用于我在几秒钟前和几年前发布的情
我目前在我的应用程序中有这个代码:

def comment_poster(comment)
    if comment.user
      "posted by #{comment.user.username} #{time_ago_in_words(comment.created_at)} ago"
    else
      "posted by anonymous"
    end
  end

但是,这只适用于我在几秒钟前和几年前发布的情况:

posted by teejay about 1 year ago
posted by teejay about 1 month ago

当我几天前编辑created_at值时,它给了我:

posted by thorpe {{count}} days ago

我应该怎么做才能使它工作并失去{{count}}

编辑:如果这意味着什么,我正在使用Rails 2.3.5

编辑:#2

我通过在config / locales / en.yml粘贴this code来解决问题

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"

任何人都可以解释这个问题是什么?

解决方法

问题出在i18n gem版本上.如果降级到0.4.1,这个问题就会消失.

问题是从版本0.4.1开始,访问变量的方式不再是{{variable}}.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读