简体   繁体   中英

undefined method `strftime' for nil:NilClass [rails 4]

To break down my app quickly: I have a songs, comments, and users model. User's can upload songs, and make comments.

I seem to be getting "undefined method `strftime' for nil:NilClass" error after submitting (creating) a song. If you go to show#songs.html.erb you'll see the comments code below. I've looked into how Ryan Bates does his comments and my code is identical. Not sure why strftime is't working. Please advise :)

Note: for what it's worth I'm running rails 4

Full error msg:

NoMethodError in Songs#show

Showing /Users/apane/Downloads/leap/app/views/songs/show.html.erb where line #34 raised:

undefined method `strftime' for nil:NilClass

Extracted source (around line #34):

<% for comment in @song.comments %>
<div class="comment">
<strong><%= link_to_unless comment.site_url.blank?, h(comment.author_name), h(comment.site_url) %></strong>
<em>on <%= comment.created_at.strftime('%b %d, %Y at %H:%M') %></em>
<%=simple_format comment.content %>
<p>
<% if can? :update, comment %>

The comment.created_at is nil - this is your problem. It is probably because you have created the comment but didn't save it yet . The created_at field will be filled only upon save.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM