2016-03-21 43 views
1

我有一個Rails應用程序,當我跑我的測試我有此錯誤消息:ActionView :: Template :: Error:SQLite3 :: SQLException no such column「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 %> 

,並提前

感謝型號EN架構

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

我認爲這是由於你在'show_html.erb'中使用'@ question_list'而不是'@ question_lists'造成的錯誤。 –

+0

謝謝,但在糾正錯字後,錯誤信息仍然存在一樣。 – timbartels

+0

此外,我認爲錯誤是在控制器的'show'方法中出現'@questions = @ question_lists.questions'。通過在'show'方法中打印'puts params'來嘗試查看作爲'id'收到的'params' –

回答

2

測試數據庫模式不同步。

強制與rake db:test:prepare重新同步。

相關問題