Invalid foreign key when deleting tags in rails

Viewed 2553

I am following this tutorial. The tutorial requires me to implement the destroy function for tags.

The tags can be created and shown fine. However, destroying them yields the following error:

 Parameters: {"authenticity_token"=>"VcYU8FRqn4oBXCv0NKXuO7yKNdI+9fIk46rY1ZwD7cQ8cqi37nZDVwNnWJLcNMWVq4gi3OU3YFDgzdeTRa1XKw==",
 "id"=>"1"}
  Tag Load (0.5ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
   (0.0ms)  begin transaction
  SQL (2.0ms)  DELETE FROM "tags" WHERE "tags"."id" = ?  [["id", 1]]
   (1.0ms)  rollback transaction
Completed 500 Internal Server Error in 10ms (ActiveRecord: 3.5ms)



ActiveRecord::InvalidForeignKey (SQLite3::ConstraintException: FOREIGN KEY constraint failed: DELETE FROM "tags" WHERE "tags"."i
d" = ?):

app/controllers/tags_controller.rb:13:in `destroy

tags_controller.rb

class TagsController < ApplicationController

    def index
        @tags = Tag.all
    end

    def show
        @tag = Tag.find(params[:id])
    end

    def destroy
        @tags = Tag.find(params[:id])
        @tags.destroy
        # Set variable to instance of an object of class Article and call .destroy on it.
    end
end

index.html.erb

<h1>Listing tags</h1>

  <% @tags.each do |tag| %>
    <tr>
      <td><%= tag.name %></td>
      <td><%= link_to 'Show', tag_path(tag) %></td>
      <td><%= link_to 'Delete', tag_path(tag),
              method: :delete,
              data: { confirm: 'Are you sure?' } %></td>
    </tr>
  <% end %>
</table>

Is there a step i missed out or something I entered wrong?

1 Answers
Related