2016-11-07 64 views
0

我有一個登錄用戶可以向帖子添加評論的表單。這是我在我的文件中的代碼,到目前爲止:如果驗證失敗,則爲評論顯示錯誤

# 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 

當我點擊提交按鈕來添加與形式是空的(需要註釋的身體),我得到這個奇怪的錯誤評論(更好的錯誤寶石)

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是預期的。

我已經暫時從這個文件中刪除了所有ruby嵌入代碼,並且一切正常 - 除了從我沒有得到評論列表的事實 - 驗證按預期工作,我可以看到我的錯誤屏幕。

我該如何解決這個問題?

編輯

工作正常,但是我在同一件事情掙扎。如果帖子已經有一些舊評論,我會收到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) ??

回答

0

正如您已經聲明「created_at ... is nil」。即使你的代碼行被註釋掉了,它仍然會被erb評估,導致time_ago_in_words拋出零例外。這將是更好的使用條件語句,如:

# 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 
+0

感謝,似乎這樣的工作方式,但是我有N + 1查詢問題,其中有老的意見後submiting形式掙扎時。我怎樣才能解決這個問題?請檢查我的代碼,我做了更新 – Lykos

+0

嗯我不知道那一個 - 我沒有太多的n + 1問題的經驗。如果你將問題恢復到原來的狀態,並提出一個新的問題,那會更好。我相信還有很多其他人在我的n + 1問題上有更多的經驗。 – David

+0

好吧,我可能會爲此創建一個新主題。再次感謝您的幫助! :-) – Lykos

相關問題