未定义的方法`strftime'代表nil:NilClass - 评论日期

时间:2016-08-26 06:13:21

标签: ruby-on-rails ruby

我正在显示评论发布的日期:

<%= 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">&times;</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>

1 个答案:

答案 0 :(得分:0)

我认为您没有正确填充本地变量注释。  试试这个,

 comment.created_at.strftime("%d %b %Y") if comment and comment.created_at