I'm trying to destroy an instance of a class (Book) but it won't work at all. Keep getting the same error messages. Here are the error messages:
ActiveRecord::StatementInvalid in BooksController#destroy
SQLite3::SQLException: no such column: chapters.book_id: SELECT "chapters".* FROM "chapters" WHERE "chapters"."book_id" = ?
Here is my code:
Models
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
Controllers
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
rails g migration add_book_id_to_chapters book:referencesthenrails db:migrate. - Sebastian Palma