gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:20:38 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com