gpt4 book ai didi

ruby-on-rails - Time_ago_in_words 不起作用

转载 作者:数据小太阳 更新时间:2023-10-29 08:37:18 26 4
gpt4 key购买 nike

我遇到了一个奇怪的问题。在我的 Rails 应用程序中,执行:time_ago_in_words homework.created_at 返回错误:

undefined method `>' for nil:NilClass

上下文:

<h3 class="media-heading"><%= homework.title %></h3>
<p>Assigned <%= time_ago_in_words homework.created_at %> ago</p>

created_at 已定义,此迁移 t.timestamps null: false(是的,我已经运行了),并且它在数据库中。

当第二行被取出时,标题起作用。

homework.created_at 返回:2015-03-08 13:45:11 -0500

跟踪:

app/views/homeworks/_homework.html.erb:6:in `_app_views_homeworks__homework_html_erb__2140072314140949314_70221661493300'
app/views/welcome/home.html.erb:26:in `block in _app_views_welcome_home_html_erb__3498543795407979710_70221651392200'
app/views/welcome/home.html.erb:24:in `_app_views_welcome_home_html_erb__3498543795407979710_70221651392200'
app/controllers/welcome_controller.rb:7:in `index'

homework.created_at.to_time 错误:undefined methodto_time' for nil:NilClass`

最佳答案

试试这个:

<%= time_ago_in_words(homework.created_at) unless homework.created_at.blank? %>

关于ruby-on-rails - Time_ago_in_words 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28931253/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com