Search code examples
ruby-on-railsnomethoderrorstrftime

No Method Error for strftime


I'm not sure where the error exists. The same code is working for a collaborator and I can't get this to work! Let me know what other code need to be shown, I'm not sure where the error is...in the db? Thanks.

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"> 

Solution

  • event.start_date is nil. You're trying to call strftime on an instance of NilClass. You want the below which will "try" and call the method, but will fail silently if the method isn't available (ie. if event.start_date is nil).

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