我遇到了一个奇怪的问题。在我的 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 method
to_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/