调用partial时出错:SQLite3 :: SQLException:没有这样的列:

时间:2016-02-03 01:12:04

标签: ruby-on-rails ruby-on-rails-4

我已经浏览了StackOverflow以获得我的错误答案,并且在试用其他问题的解决方案时没有任何运气。我对Rails相当陌生,而且我正在做一个待办事项列表应用项目。当我尝试查看todo_list#show视图时,我在第2行收到以下错误:

SQLite3::SQLException: no such column: todo_items.todo_list_id: SELECT "todo_items".* FROM "todo_items" WHERE "todo_items"."todo_list_id" = ?

显示视图

<div id="todo_items_wrapper">
   <p><%= render @todo_list.todo_items %></p> #this line is highlighted as the error
<div id="form">
    <%= render "todo_items/form" %>
   </div>
</div>

我试图在第2行调用以下部分内容;这是部分文件:

<%= form_for([@todo_list, @todo_list.todo_items.build]) do |f| %>
  <%= f.text_field :content, placeholder: "New Todo" %>
  <%= f.submit %>
<% end %>

我假设我的迁移存在问题,因为我的错误是说明了一个缺失的列,但我不知道如何解决这个问题。如果需要,下面是我的迁移文件:

迁移文件

class CreateTodoLists < ActiveRecord::Migration
  def change
    create_table :todo_lists do |t|
      t.string :title
      t.text :description

      t.timestamps null: false
    end
  end
end

并...

class CreateTodoItems < ActiveRecord::Migration
  def change
    create_table :todo_items do |t|
      t.string :content
      t.references :todo_lists, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

模式

ActiveRecord::Schema.define(version: 20160203002224) do

  create_table "todo_items", force: :cascade do |t|
    t.string   "content"
    t.integer  "todo_lists_id"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
  end

  add_index "todo_items", ["todo_lists_id"], name: "index_todo_items_on_todo_lists_id"

  create_table "todo_lists", force: :cascade do |t|
    t.string   "title"
    t.text     "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

end

如果需要任何其他文件来帮助我解决错误,请告诉我。谢谢!

1 个答案:

答案 0 :(得分:0)

错误是由于迁移文件中的名称错误(todo_lists,它应该是todo_list)

 t.references :todo_lists, index: true, foreign_key: true

应该是

 t.references :todo_list, index: true, foreign_key: true

您可以编写一个迁移来重命名列名来修复它。

相关问题