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

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

发布时间:2020-12-16 19:34:24 所属栏目:百科 来源:网络整理
导读:我在记录上有一个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_wor
我在记录上有一个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"

(编辑:李大同)

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

    推荐文章
      热点阅读