ActionView :: Template ::错误:SQLite3 :: SQLException没有这样的列“question_list_id”=?

时间:2016-03-21 09:45:48

标签: ruby-on-rails sqlite capybara minitest

我有一个Rails应用程序,当我运行测试时,我收到了以下错误消息:

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

提前致谢!

1 个答案:

答案 0 :(得分:3)

测试数据库架构不同步。

强制与rake db:test:prepare重新同步。