【问题标题】:Working with distance_of_time_in_words in Rails 3在 Rails 3 中使用 distance_of_time_in_words
【发布时间】:2011-12-02 11:42:40
【问题描述】:

我正在尝试使用 Rail 的 distance_of_time_in_words 帮助程序,但由于某种原因我收到了未定义的方法错误。这是我的代码:

def confirm_has_quota
  last_upload = current_user.photos.last.created_at
  remaining_time = distance_of_time_in_words(1.day.ago, last_upload)
  if last_upload < 1.day.ago
    return true
  else
    flash[:error] = "You are allowed 1 upload per day. Please try again in" + remaining_time + "."
    redirect_to(:back)
  end
end

这给了我“未定义的方法 `distance_of_time_in_words'”。有人看到我在这里做错了吗?谢谢。

【问题讨论】:

    标签: time


    【解决方案1】:

    distance_of_time_in_words 方法是一个 ActionView Helper,因此需要从 View(而不是控制器)调用。

    http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html

    【讨论】:

      【解决方案2】:

      或者您可以通过控制器内部可用的 view_context 访问相同的内容。

      view_context.distance_of_time_in_words
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-27
        • 2013-12-18
        • 1970-01-01
        • 2010-11-16
        相关资源
        最近更新 更多