【问题标题】:No Method Error for strftimestrftime 没有方法错误
【发布时间】:2012-11-09 05:30:50
【问题描述】:

我不确定错误出现在哪里。相同的代码正在为合作者工作,我无法让它工作!让我知道需要显示哪些其他代码,我不确定错误在哪里......在数据库中?谢谢。

NoMethodError in Static_pages#home
undefined method `strftime' for nil:NilClass

...views/shared/_event_detail.html.erb

         </span>
       </div>
       <div class="span6 start_date">
         <%=event.start_date.strftime('%B %d, %Y') %>
       </div>
     </div>
     <div class="row-fluid"> 

【问题讨论】:

    标签: ruby-on-rails nomethoderror strftime


    【解决方案1】:

    event.start_date 为零。您正在尝试在 NilClass 的实例上调用 strftime。您希望下面的内容将“尝试”并调用该方法,但如果该方法不可用(即如果 event.start_date 为 nil)将静默失败。

         <%=event.start_date.try(:strftime, '%B %d, %Y') %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 2021-10-12
      • 2018-04-05
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多