2016-03-21 4 views
1

私はRailsアプリケーションを持っていると私は私のテストを実行したときに、私はこのエラーメッセージを持っている:ActionView :: Template :: Error:SQLite3 :: SQLExceptionこのような列はありません "question_list_id" =?

Error: QuestionListsControllerTest#test_should_show_question_list: ActionView::Template::Error: 
SQLite3::SQLException: no such column: questions.question_list_id: SELECT "questions".* FROM "questions" WHERE "questions"."question_list_id" = ? 
    app/views/question_lists/show.html.erb:8:in `_app_views_question_lists_show_html_erb__3832013936113844388_70290298491920' 
    test/controllers/question_lists_controller_test.rb:28:in `block in <class:QuestionListsControllerTest>' 

これは私のコントローラです:

class QuestionListsController < ApplicationController 
before_action :set_question_list, only: [:show, :edit, :update, :destroy] 

    def index 
    @question_lists = QuestionList.all 
    end 

    def show 
    @questions = @question_list.questions 
    end 

    private 
    def set_question_list 
    @question_list = QuestionList.find(params[:id]) 
    end 

    def question_list_params 
    params.require(:question_list).permit(:title) 
    end 
end 

これは私のテストファイルです:

require 'test_helper' 

class QuestionListsControllerTest < ActionController::TestCase 
    setup do 
    @question_list = question_lists(:one) 
    end 

    test 'should get index' do 
    get :index 
    assert_response :success 
    assert_not_nil assigns(:question_lists) 
    end 
test 'should show question_list' do 
    get :show, id: @question_list 
    assert_response :success 
    end 
end 

これは私のshow.html.erbです

<p id="notice"><%= notice %></p> 

<p> 
    <h1>Question List: <%= @question_list.title %></h1> 
</p> 

<h2>Questions</h2> 
<% @questions.each do |question| %> 
    <p> 
    <strong>Question:</strong> 
    <%= question.title %> 
    </p> 
<% end %> 

<h2>Create a question</h2> 
<%= form_for([@question_list, @questions.build]) do |f| %> 
    <p> 
    <%= f.label :title, "Question:" %> 
    <%= f.text_field :title %> 
    </p> 
    <p> 
    <%= f.submit "Create Question"%> 
    </p> 
<% end %> 
<%= link_to 'Edit', edit_question_list_path(@question_list) %> | 
<%= link_to 'Back', question_lists_path %> 

そして

おかげで事前にスキーマ

class QuestionList < ActiveRecord::Base 
    has_many :questions 
end 

class Question < ActiveRecord::Base 
    belongs_to :question_list 
end 

Schema.rb

ActiveRecord::Schema.define(version: 20160316111127) do 

    create_table "question_lists", force: :cascade do |t| 
    t.string "title" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    create_table "questions", force: :cascade do |t| 
    t.string "title" 
    t.integer "question_list_id" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    end 

end 
専用モデル!

+0

'@ question_list 'を使うのではなく、' @ question_list'を使った 'show.html.erb'の入力ミスであると思います。 –

+0

ありがとうございますが、誤植を訂正しても、同じ。 – timbartels

+0

また、エラーはあなたのコントローラの 'show'メソッドの' @questions = @ question_lists.questions'であると思います。 'show'メソッドに' puts params'のような 'params'を出力することで、' id'として受け取った 'params'を見てみましょう。 –

答えて

2

テストデータベースのスキーマが同期していません。

rake db:test:prepareとの再同期を強制します。

関連する問題