ruby-on-rails – 为什么日期助手在Rails控制台中不起作用?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 为什么日期助手在Rails控制台中不起作用?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在记录上有一个datetime属性
1.9.3p194 :024> f.last_contact
 => Thu,11 Aug 2011 00:00:00 UTC +00:00

但是当我在控制台上尝试使用time_ago_in_words时,它不起作用:

> time_ago_in_words(f.last_contact)
NoMethodError: undefined method `time_ago_in_words' for main:Object

我也尝试了distance_of_time_in_words,文档说应该与Time,Date & DateTime objects配合使用.

> to_time = Time.now
 => 2012-09-08 12:22:16 -0500 
> distance_of_time_in_words(f.last_contact,to_time)
NoMethodError: undefined method `distance_of_time_in_words' for main:Object

这是什么原因? Rails控制台不应该加载所有Rails方法所需的库和依赖项吗?

解决方法

您可以通过帮助对象使用所有帮助(内置和自己)
helper.time_ago_in_words(1.hour.ago)
=> "about 1 hour"

或导入所需的助手

include ActionView::Helpers::DateHelper
time_ago_in_words(1.hour.ago)
=> "about 1 hour"
原文链接:https://www.f2er.com/ruby/271379.html

猜你在找的Ruby相关文章