2014-08-28 78 views
0

我踐行着CRUD TDD和我遇到了此問題:未定義的局部變量或方法`question_params',儘管它被定義

undefined local variable or method `question_params' for #<QuestionsController:0x0000010618a248> 

這裏是我的規格(雖然我認爲他們是不相關的):

describe '#create' do 
    it 'assigns @question to a new question' do 
    post :create 
    expect(assigns(:question)).to be_a_new(Question) 
    end 

    it 'redirects to the question once created' do 
    post :create 
    expect(response).to redirect_to question_path(@question) 
    end 
end 

這裏是控制器:

class QuestionsController < ApplicationController 
    def new 
    @question = Question.new 
    end 

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

    def create 
    @question = Question.new(question_params) 

    if @question.save 
     redirect_to @question 
    else 
     render :new 
    end 

    private 

    def question_params 
     params.require(:question).permit(:title, :body) 
    end 
    end 
end 

這是怎麼回事?

回答

2

您在其他方法create內定義question_params。它應該看起來像這樣:

class QuestionsController < ApplicationController 
    def new 
    @question = Question.new 
    end 

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

    def create 
    @question = Question.new(question_params) 

    if @question.save 
     redirect_to @question 
    else 
     render :new 
    end 
    end 

private 

    def question_params 
    params.require(:question).permit(:title, :body) 
    end 
end 
+0

哦,天哪。 ** **捂臉 – 2014-08-28 02:50:43

1

您的目標不排隊。創建方法當前包括question_params方法。

試試這個:

def create 
    @question = Question.new(question_params) 

    if @question.save 
    redirect_to @question 
    else 
    render :new 
    end 
end 

private 

def question_params 
    params.require(:question).permit(:title, :body) 
end 
相關問題