2013-06-07 2 views
0

게시 모델에 주석을 추가하려고합니다. 나는이 오류가덧글을 작성하는 방법

class CommentsController < ApplicationController 

    before_action :find_post 

    def index 
     @comments = @post.comments.order('id') 
    end 

    def new 
     @comment = @post.comments.new 
    end 

    def create 
     @comment = @post.comments.build(params[:comment]) 
     @comment.user = current_user 
     @comment.save 
     redirect_to post_path(@post) 
    end 

    private 
    def comment_params 
     params.require(:comment).permit(:comment_body) 
    end 

    def find_post 
     @user = current_user 
     @post = @user.posts.find(params[:post_id]) 
    end 
end 

:이 지금까지 내 comments_controller입니다

NoMethodError in CommentsController#new undefined method `posts' for nil:NilClass

당신은 내가 프로그래밍에 새로 온 사람을 볼 수 있습니다. 도움을 주셔서 감사합니다! :)

답변

1

아마 before_action :find_post 때문에 find_post 메서드가 호출됩니다. 로그인하지 않은 경우 아마도 오류가 발생합니다. 로그인하지 않은 경우 current_usernil이고 nilposts입니다.

+0

예! 고맙습니다 :) – fuskie