在Rails 3中使用distance_of_time_in_words

Ton*_*ate 2 time

我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)

这给了我"未定义的方法`distance_of_time_in_words'".有谁看到我在这里做错了什么?谢谢.

Jon*_*osh 7

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

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


tha*_*kal 5

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

view_context.distance_of_time_in_words
Run Code Online (Sandbox Code Playgroud)