快速分解我的應用程序:我有一首歌曲,評論和用戶模型。用戶可以上傳歌曲並發表評論。未定義的方法'strftime'爲零:NilClass [rails 4]
我似乎在提交(創建)一首歌曲後出現「未定義的方法`strftime'爲零:NilClass」錯誤。如果你去show#songs.html.erb你會看到下面的評論代碼。我研究了瑞恩貝茨如何評論他,並且我的代碼是相同的。不知道爲什麼strftime不工作。請指教:)
注:這是非常值得我運行軌道4
完整的錯誤消息:
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 %>
它的'created_at'不起作用。可能的原因是你沒有自動填充字段(所以它在'Comment'模型中是數據庫中的'NULL',Ruby中'nil')。 –
尼爾,我如何自動填充它?有沒有我應該添加到控制器的代碼行? – Apane101