无法破坏Rails类的实例,SQL错误

时间:2018-02-01 00:35:36

标签: sql ruby-on-rails ruby

我试图摧毁一个班级(书籍)的实例,但它根本没有工作。继续收到相同的错误消息。以下是错误消息:

BooksController中的ActiveRecord :: StatementInvalid#destroy

SQLite3 :: SQLException:没有这样的列:chapters.book_id:SELECT"章节"。* FROM"章节"在哪里"章节"。" book_id" =?

这是我的代码:

模型

class Book < ApplicationRecord
 has_many :chapters, dependent: :destroy
 has_attached_file :bookcover, styles: { medium: "300x300>", thumb: "100x100>" }
 has_attached_file :authorpic, styles: { medium: "300x300>", thumb: "100x100>" }
 validates_attachment_content_type :bookcover, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
 validates_attachment_content_type :authorpic, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
 validates :title, presence: true,
                length:{minimum: 5}
end


class Chapter < ApplicationRecord
 has_many :sections, dependent: :destroy
 belongs_to :book
 validates :title, presence: true,
                length:{minimum: 5}
end

控制器

class BooksController < ApplicationController
  def show
     @book =Book.find(params[:id])
  end

  def index
     @books = Book.all
  end

  def new
    @book = Book.new
  end

  def edit
    @book = Book.find(params[:id])
  end

  def create
   @book =  Book.new(book_params)

   if @book.save
     redirect_to @book
   else
     render 'new'
   end
  end

  def update
   @book = Book.find(params[:id])

   if @book.update(book_params)
    redirect_to @book
   else
    render 'edit'
   end
  end

 def destroy
  @book = Book.find(params[:id])
  @book.destroy

  redirect_to books_path
 end

 private
  def book_params

   params.require(:book).permit(:title,:text,:bookcover,:authorpic,:author)
  end
 end

schema.rb

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

  create_table "bookcomments", force: :cascade do |t|
    t.string "commenter"
    t.text "body"
    t.integer "section_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["section_id"], name: "index_bookcomments_on_section_id"
  end

  create_table "books", force: :cascade do |t|
    t.string "title"
    t.text "text"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "bookcover_file_name"
    t.string "bookcover_content_type"
    t.integer "bookcover_file_size"
    t.datetime "bookcover_updated_at"
    t.string "authorpic_file_name"
    t.string "authorpic_content_type"
    t.integer "authorpic_file_size"
    t.datetime "authorpic_updated_at"
    t.string "author"
    t.string "month"
  end

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

  create_table "ckeditor_assets", force: :cascade do |t|
    t.string "data_file_name", null: false
    t.string "data_content_type"
    t.integer "data_file_size"
    t.string "data_fingerprint"
    t.string "type", limit: 30
    t.integer "width"
    t.integer "height"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["type"], name: "index_ckeditor_assets_on_type"
  end

  create_table "comments", force: :cascade do |t|
    t.string "commenter"
    t.text "body"
    t.integer "essay_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["essay_id"], name: "index_comments_on_essay_id"
  end

  create_table "contacts", force: :cascade do |t|
    t.string "name"
    t.string "email"
    t.text "comments"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

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

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

  create_table "podcomments", force: :cascade do |t|
    t.string "commenter"
    t.text "body"
    t.integer "podcast_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["podcast_id"], name: "index_podcomments_on_podcast_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

end

1 个答案:

答案 0 :(得分:0)

在我看来,你的章节表应该引用它所属的书。删除该书需要删除章节 你有依赖::破坏线。哎呀。我看到塞巴斯蒂安先给出了这个答案!喝彩!