ruby-on-rails - ActionView::Template::错误:SQLite3::SQLException 没有这样的列 "question_list_id"=?

标签 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>'

这是我的 Controller :

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

架构.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

提前致谢!

最佳答案

测试数据库架构不同步。

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

关于ruby-on-rails - ActionView::Template::错误:SQLite3::SQLException 没有这样的列 "question_list_id"=?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36127581/

相关文章:

android - Realm 比 SQLite 花费更多时间进行读写

html - 如何检查复选框是否被选中 capybara Rspec

ruby-on-rails - 为什么 Rails 服务器会出现 "Could not find ' railties 错误?

ruby-on-rails - 在 Rails 控制台中禁用数据库写入(仅针对当前 session )?

database - 巨大的数据存储问题

php - 单个 SQL 查询中的百分比

ruby-on-rails - 通过 Capybara (v2) 与 Bootstrap 模态交互时出现问题

ruby-on-rails - 使用 capybara 进行 cucumber 测试中的 cookies

ruby-on-rails - Rspec 中的标签可以进行 AND 运算还是 OR 运算?

ruby-on-rails - Rails 3 根路由问题?