I am new to Ruby on Rails. I try to make tags and have the option to delete the tag. When I want to destroy the tag, I get this error SQLite3::ConstraintException: FOREIGN KEY constraint failed.
ActiveRecord::InvalidForeignKey in TagsController#destroy
Here are my code for delete tag in controller:
def destroy
@tag = Tag.find(params[:id])
@tag.destroy
flash.notice = "Tag '#{@tag.name}' Deleted!"
redirect_to tags_path
end
This is the schema.rb
ActiveRecord::Schema.define(version: 2021_03_20_115719) do
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "comments", force: :cascade do |t|
t.string "author_name"
t.text "body"
t.integer "article_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["article_id"], name: "index_comments_on_article_id"
end
create_table "taggings", force: :cascade do |t|
t.integer "tag_id", null: false
t.integer "article_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["article_id"], name: "index_taggings_on_article_id"
t.index ["tag_id"], name: "index_taggings_on_tag_id"
end
create_table "tags", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
add_foreign_key "comments", "articles"
add_foreign_key "taggings", "articles"
add_foreign_key "taggings", "tags"
end