2015-06-01 54 views
0

只是我正在創建的應用程序徵求意見,與多態-協會rails.I創建問題控制器,控制器回答和評論控制器幾乎我做了所有我的工作應用程序,但我在創建評論時出錯。你能幫我麼?低於我的評論控制器代碼。未定義的方法`評論的零:NilClass在Rails應用程序

class AnswersController < ApplicationController 
    before_action :set_answer, only: [:show, :edit, :update, :destroy] 

    def index 
    @answers = Answer.all 
    end 

    def show 
    end 

    def new 
    @answer = Answer.new 
    end 

    def edit 
    end 

    def create 
    @answer = Answer.new(answer_params) 

    respond_to do |format| 
     if @answer.save 
     format.html { redirect_to @answer, notice: 'Answer was successfully created.' } 
     end 
    end 
    end 

    def update 
    respond_to do |format| 
     if @answer.update(answer_params) 
     format.html { redirect_to @answer, notice: 'Answer was successfully updated.' } 
     end 
    end 
    end 

    def destroy 
    @answer.destroy 
    respond_to do |format| 
     format.html { redirect_to answers_url, notice: 'Answer was successfully destroyed.' } 
    end 
    end 

    private 
    def set_answer 
     @answer = Answer.find(params[:id]) 
    end 

    def answer_params 
     params.require(:answer).permit(:answer) 
    end 
end 

class QuestionsController < ApplicationController 
    before_action :set_question, only: [:show, :edit, :update, :destroy] 

    def index 
    @questions = Question.all 
    end 

    def show 
    end 

    def new 
    @question = Question.new 
    end 

    def edit 
    end 

    def create 
    @question = Question.new(question_params) 

    respond_to do |format| 
     if @question.save 
     format.html { redirect_to @question, notice: 'Question was successfully created.' } 
     end 
    end 
    end 

    def update 
    respond_to do |format| 
     if @question.update(question_params) 
     format.html { redirect_to @question, notice: 'Question was successfully updated.' } 
     end 
    end 
    end 

    def destroy 
    @question.destroy 
    respond_to do |format| 
     format.html { redirect_to questions_url, notice: 'Question was successfully destroyed.' } 
    end 
    end 

    private 

    def set_question 
     @question = Question.find(params[:id]) 
    end 

    def question_params 
     params.require(:question).permit(:question) 
    end 
end 

class CommentsController < ApplicationController 
    before_action :set_comment, only: [:show, :edit, :update, :destroy, :create] 


    def create 
    @comment = @commentable.comments.new comment_params 
    @comment.user = current_user 
    @comment.save 
    redirect_to @commentable, notice: "Your comment was successfully posted!" 
    end 

    private 


    def comment_params 
     params.require(:comment).permit(:body) 
    end 
end 
controller/questions/comments_controller.rb 

class Question::CommentsController < CommentsController 
    before_action :set_commentable, only: [:show, :edit, :update, :destroy, :create] 

    private 

    def set_commentable 
     @commentable = Question.find(params[:question_id]) 
     @commentable = Comment.find(params[:comment_id]) 
    end 
end 


controller/answers/comments_controller.rb 

class Answer::CommentsController < CommentsController 
    before_action :set_commentable, only: [:show, :edit, :update, :destroy, :create] 

    private 

    def set_commentable 
     @commentable = Answer.find(params[:answer_id]) 
     @commentable = Comment.find(params[:comment_id]) 
    end 
end 

現在我得到這個錯誤

undefined method `set_comment' for #<CommentsController:0xb34e3348> 
+1

您創建您正在獲取@commentable的動作值是nill –

+1

您需要在您的CommentsController中設置@commentable –

+0

如何在CommentsController中設置@commentable – prasanthrubyist

回答

0

既然你沒有set_commentable你CommentsController裏面你越來越未定義的方法錯誤信息:

before_action :set_comment, only: [:show, :edit, :update, :destroy, :create] 
private 

def set_comment 
    @commentable = Comment.find(params[:id]) 
end 
+0

註釋不適用於擴展討論;這次談話一直[移動聊天](​​http://chat.stackoverflow.com/rooms/79324/discussion-on-answer-by-adnan-devops-undefined-method-comments-for-nilnilclas)。 – Taryn

0

在您的評論添加此控制器

def set_comment 
    @comment = Comment.find(params[:id]) 
end 
+0

不能正常工作.... – prasanthrubyist

相關問題