Issues with rails blogger assignment (tags)


#1

I’m at an utter loss here. In my tags controller, the method I have for destroy is (virtually the same as the articles one):
def destroy
@tag = Tag.find(params[:id])
@tag.destroy

flash.notice = "Tag '#{@tag.name}' Deleted!"

redirect_to tag_path

end

However I get an error when I actually attempt to destroy a tag, stating: SQLite3::ConstraintException: FOREIGN KEY constraint failed: DELETE FROM “tags” WHERE “tags”.“id” = ?

I’m not quite sure where this foreign key issue is originating, nor how to fix it. Any help would be greatly appreciated.


#2

Hey @markb

In your tags model the line for has_many taggings needs to be changed to

has_many :taggings, dependent: :destroy

I think that’s correct but I’m on my phone so can’t be 100% sure. Essentially when you delete a tag, Rails wants to force the deletion of any associated tags in taggings, otherwise if you delete a tag then you could still have taggings that reference tags that don’t exist. It wasn’t always this way so the tutorial is a little out of date and will hopefully be updated shortly.


#3

Thank you - that fixed the current error message, but now I get ‘Couldn’t find Tag with ‘id’=[whatever id # the tag currently is]’

It is in fact deleting the tags now, but I am receiving the error upon rails calling the tag controller show method. It’s trying to show a tag that, once deleted, no longer exists. I can’t for the life of me figure out why it’s trying to show tags that no longer exist. Shouldn’t @tag.articles.each do etc… not have that tag in its array?


#4

Is the controller trying to route back to the tag after deleting it?


#5

You sir, are a genius. Thank you so much. That was absolutely it. @CouchofTomato