制作一个待办事项 webapp 并得到这样的错误 SQLite3::SQLException: no such column: tasks.list_id
Making a to-do webapp and got such an error SQLite3::SQLException: no such column: tasks.list_id
显示 /home/arthur/Desktop/application/todo/app/views/tasks/index.html.erb,其中第 7 行 raised:SQLite3::SQLException:没有这样的列:tasks.list_id
当我尝试打开创建的列表以添加新任务时出现此错误。我认为问题是应用程序无法获取我要打开的列表的 ID。但我不明白出了什么问题。 (新增 ruby 和 rails)。我需要添加视图或其他内容吗?
任务控制器
class TasksController < ApplicationController
helper_method :task, :list, :tasks
def create
task.save
redirect_to list_tasks_path(list)
end
def update
task.update task_params
redirect_to list_tasks_path(list)
end
def complete
task.complete
redirect_to list_tasks_path(list)
end
def destroy
task.destroy
redirect_to list_tasks_path(list)
end
private
delegate :tasks, to: :list, private: true
def task
@task ||= params[:id] ? list.tasks.find(params[:id]) : list.tasks.new(task_params)
end
def list
@list ||= List.find params[:list_id]
end
def task_params
params.require(:task).permit(:title, :completed)
end
end
和 ListsController
class ListsController < ApplicationController
#helper_method :list, :lists
def index
@lists = List.order('created_at')
end
def new
@list = List.new
end
def create
@list = List.new list_params
@list.save
redirect_to lists_path
end
def edit
@list = List.find params[:id]
redirect_to lists_path
end
def update
@list = List.find params[:id]
@list.update list_params
redirect_to lists_path
end
def destroy
@list = List.find params[:id]
@list.destroy
redirect_to lists_path
end
private
def list_params
params.require(:list).permit(:title)
end
end
查看索引。html.erb
<div class="col-md-12">
<h2>Tasks</h2>
<div class="buffer-top"><%= link_to "New Task", new_list_task_path(list), class: 'btn btn-primary' %></div>
<div class="buffer-top">
<% tasks.each do |task| %> <----------- this line
<%= render partial: 'task', object: task %>
<% end %>
</div>
<div class="buffer-top"><%= link_to "Back to Lists", lists_path, class: 'btn btn-primary btn-sm' %></div>
</div>
class Task < ApplicationRecord
belongs_to :lists
def complete!
self.completed = true
save
end
end
class List < ApplicationRecord
has_many :tasks
end
架构
ActiveRecord::Schema.define(version: 2020_04_06_202846) do
create_table "lists", force: :cascade do |t|
t.string "title"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "tasks", force: :cascade do |t|
t.string "title"
t.boolean "completed"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end
您的任务模型中有 belongs_to :lists
,但相应的 tasks
table 中没有 list_id
。您的任务 table 需要一个包含 t.integer "list_id", :null => false
的迁移。
create_table "tasks", force: :cascade do |t|
t.integer "list_id", :null => false
t.string "title"
t.boolean "completed"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
显示 /home/arthur/Desktop/application/todo/app/views/tasks/index.html.erb,其中第 7 行 raised:SQLite3::SQLException:没有这样的列:tasks.list_id
当我尝试打开创建的列表以添加新任务时出现此错误。我认为问题是应用程序无法获取我要打开的列表的 ID。但我不明白出了什么问题。 (新增 ruby 和 rails)。我需要添加视图或其他内容吗?
任务控制器
class TasksController < ApplicationController
helper_method :task, :list, :tasks
def create
task.save
redirect_to list_tasks_path(list)
end
def update
task.update task_params
redirect_to list_tasks_path(list)
end
def complete
task.complete
redirect_to list_tasks_path(list)
end
def destroy
task.destroy
redirect_to list_tasks_path(list)
end
private
delegate :tasks, to: :list, private: true
def task
@task ||= params[:id] ? list.tasks.find(params[:id]) : list.tasks.new(task_params)
end
def list
@list ||= List.find params[:list_id]
end
def task_params
params.require(:task).permit(:title, :completed)
end
end
和 ListsController
class ListsController < ApplicationController
#helper_method :list, :lists
def index
@lists = List.order('created_at')
end
def new
@list = List.new
end
def create
@list = List.new list_params
@list.save
redirect_to lists_path
end
def edit
@list = List.find params[:id]
redirect_to lists_path
end
def update
@list = List.find params[:id]
@list.update list_params
redirect_to lists_path
end
def destroy
@list = List.find params[:id]
@list.destroy
redirect_to lists_path
end
private
def list_params
params.require(:list).permit(:title)
end
end
查看索引。html.erb
<div class="col-md-12">
<h2>Tasks</h2>
<div class="buffer-top"><%= link_to "New Task", new_list_task_path(list), class: 'btn btn-primary' %></div>
<div class="buffer-top">
<% tasks.each do |task| %> <----------- this line
<%= render partial: 'task', object: task %>
<% end %>
</div>
<div class="buffer-top"><%= link_to "Back to Lists", lists_path, class: 'btn btn-primary btn-sm' %></div>
</div>
class Task < ApplicationRecord
belongs_to :lists
def complete!
self.completed = true
save
end
end
class List < ApplicationRecord
has_many :tasks
end
架构
ActiveRecord::Schema.define(version: 2020_04_06_202846) do
create_table "lists", force: :cascade do |t|
t.string "title"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "tasks", force: :cascade do |t|
t.string "title"
t.boolean "completed"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end
您的任务模型中有 belongs_to :lists
,但相应的 tasks
table 中没有 list_id
。您的任务 table 需要一个包含 t.integer "list_id", :null => false
的迁移。
create_table "tasks", force: :cascade do |t|
t.integer "list_id", :null => false
t.string "title"
t.boolean "completed"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end