-2
我顯示當評論發佈日期:未定義的方法`strftime的」爲無:NilClass - 評論日期
<%= comment.created_at.strftime("%d %b %Y") %>
這工作,但如果註釋沒有正確填寫,提交。如果發生這種情況,我得到錯誤:undefined method
strftime'爲nil:NilClass`而不是我的評論驗證。
如何解決這個問題,我不確定?
這裏是我的代碼:
class CommentsController < ApplicationController
def create
@post = Post.friendly.find(params[:post_id])
@comment = @post.comments.create(params[:comment].permit(:name, `:email, :website, :body, :url))`
if @comment.errors.any?
render "posts/show"
else
redirect_to post_path(@post)
end
end
def destroy
@post = Post.friendly.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
end
class Comment < ActiveRecord::Base
belongs_to :post
validates_format_of :email, :with => /@/
validates :name, presence: true, length: { minimum: 5}
validates :body, presence: true, length: { minimum: 5}
validates :body, presence: true, length: { minimum: 5}
def gravatar_url
stripped_email = email.strip
downcased_email = stripped_email.downcase
hash = Digest::MD5.hexdigest(downcased_email)
default_url = "https://s3-us-west-2.amazonaws.com/mirror-communications/assets/download.jpg"
"http://gravatar.com/avatar/#{hash}"
end
end
<!-- Post Comments Partial -->
<li>
<article class="comment">
<div class="user-avatar">
<%= image_tag comment.gravatar_url %>
</div>
<div class="comment-content">
<h5 class="name"><%= comment.name %></h5>
<div class="comment-meta comment-reply">
<span class="post-date"><%= comment.created_at.strftime("%d %b %Y") %></span>/<span class="comment-reply-link"><%= link_to comment.website.try(:remove, /.*\/\//), url_for(comment.website), target: '_blank', class: "comment-reply" %></span>
</div>
<p><%= comment.body %></p>
</div>
</article>
<!-- Form Partial -->
<%= form_for([@post, @post.comments.build]) do |f| %>
<% if @comment && @comment.errors.any? %>
<div id="error_explanation" class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h2><%= pluralize(@comment.errors.count, "error") %> Your comment could not be submitted:</h2>
<ul>
<% @comment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<form class="comment-form">
<div class="row">
<div class="column width-4">
<%= f.text_field :name, class: "form-name form-element", placeholder: "Name*", :tabindex => 1 %>
</div>
<div class="column width-4">
<%= f.email_field :email, class: "form-email form-element", placeholder: "Email* (not displayed)", :tabindex => 3 %>
</div>
<div class="column width-4">
<%= f.url_field :website, class: "form-website form-element", placeholder: "Website", :tabindex => 4 %>
</div>
</div>
<div class="row">
<div class="column width-12">
<%= f.text_area :body, class: "form-message form-element", placeholder: "Message*", :tabindex => 5 %>
<%= f.submit class: "form-submit button medium bkg-black bkg-hover-turquoise color-white color-hover-white no-margin-bottom" %>
</div>
</div>
</form>
</div>
<% end %>
<!-- Posts Show -->`
<div class="post-comments">
<h3 class="comments-title">
<% if @post.comments.count < 2 %>
<%= @post.comments.count %> Comment</h3>
<% else %>
<%= @post.comments.count %> Comments</h3>
<% end %>
<div class="comments">
<ul class="comment-list">
<%= render @post.comments %>
</ul>
</div>
</div>
能否詳細說一下「f評論沒有正確提交,並提交」?這些評論如何創建?他們如何堅持你的數據庫? – kiddorails
試試這個,comment.created_at.strftime(「%d%b%Y」)if comment.created_at.present? – Navin
@bgilbank,您可以在註釋未正確創建時解釋上下文嗎?如果評論中的驗證通過,評論記錄中的'created_at'和'updated_at'字段將自動填充。即。如果評論存在,您可以顯示'created_at'字段。在你看來,'comment'看起來是'nil'。試着找出爲什麼它的零。 –