如果validation失败,则显示注释错误

我有一个表单,登录用户可以在post中添加评论。 这是我的文件中的代码,到目前为止:

# views/posts/show.html.haml %h3 This post has #{ pluralize(@post.comments.count, "comment") } = render @post.comments # views/comments/_comment.html.haml = render "comments/form" # views/comments/_comment.html.haml - list all comments of the post .media .media-body %h4.media-heading = comment.user.name %small added #{time_ago_in_words comment.created_at} ago %p= comment.body %hr # views/comments/_form.html.haml - if user_signed_in? = errors_for @comment if @comment # errors_for is just a custom helper I've created to loop through all validation errors = form_for([@post, Comment.new]) do |f| .form-group = f.label :body, "Leave a comment" = f.text_area :body, class: "form-control", rows: "6" .form-group = f.submit "Add", class: "btn btn-primary" # controllers/comments_controller.rb def create @post = Post.find(params[:post_id]) @comment = @post.comments.create(params[:comment].permit(:body).merge(:user => current_user)) if @comment.errors.any? render "posts/show" else redirect_to post_path(@post) end # have also tried this way too # @comment = @post.comments.build(params[:comment].permit(:body)) # @comment.user = current_user # if @comment.save # redirect_to post_path(@post) # else # render "posts/show" # end end 

当我单击提交按钮添加注释表单为空(评论正文是必需的)时,我得到这个奇怪的错误(更好的错误gem)

 NoMethodError at /posts/1/comments undefined method `>' for nil:NilClass 

并在_comment.html.haml突出显示这行代码%small added #{time_ago_in_words comment.created_at} ago 。 此外,postID和userID按预期分配给@comment,并且comment.body和created_at以及updated_at是nill,如预期的那样。

我暂时删除了这个文件中的所有ruby嵌入代码,一切正常 – 除了我没有得到注释列表的事实 – validation按预期工作,我可以在屏幕上看到错误。

我怎样才能解决这个问题?

编辑

工作得很好,但我还在努力再做一件事。 如果post已经有一些旧的评论,我收到N + 1查询正在运行的警告。

 N+1 Query detected Comment => [:user] Add to your finder: :includes => [:user] N+1 Query method call stack /app/views/comments/_comment.html.haml:5:in `_app_views_comments__comment_html_haml__3499194119219243109_33739760' /app/views/posts/show.html.haml:23:in `_app_views_posts_show_html_haml__2205508389884571191_36379560' /app/controllers/comments_controller.rb:10:in `create' /app/views/comments/_comment.html.haml:5:in `_app_views_comments__comment_html_haml__3499194119219243109_33739760' /app/views/posts/show.html.haml:23:in `_app_views_posts_show_html_haml__2205508389884571191_36379560' /app/controllers/comments_controller.rb:10:in `create' 

我知道这不是主题,但任何想法如何解决这个问题? 我究竟需要在哪里使用.includes(:user) ??

正如您已经说过“created_at … is nil”。 即使您的代码行被注释掉,它仍然会被erb评估,导致time_ago_in_words抛出零exception。 最好使用条件语句,例如:

 # views/comments/_comment.html.haml - list all comments of the post .media .media-body %h4.media-heading = comment.user.name - if comment.created_at.present? %small added {time_ago_in_words comment.created_at} ago - else % small comment not yet been created %p= comment.body %hr