turingschool / curriculum

Turing's main repository of tutorials and projects
385 stars 502 forks source link

Blogger 2 - I3 Tagging, final step #1249

Open mn113 opened 6 years ago

mn113 commented 6 years ago

I'm not sure if this applies to Rails 5 or only an older version that the Blogger tutorial was created for.

Now that we can see all of our tags, we also want the capability to delete them. I think you can do this one on your own too. Create a destroy action in your tags_controller.rb and edit the index.html.erb file you just created. Look at your articles_controller.rb and Article show.html.erb if you need some clues. With that, a long Iteration 3 is complete!

During this DIY step, a foreign key error was raised when I tried to delete a Tag the same way as taught for Articles. ActiveRecord::InvalidForeignKey (SQLite3::ConstraintException: FOREIGN KEY constraint failed: DELETE FROM "tags" WHERE "tags"."id" = ?):

I solved it by (as on SO: https://stackoverflow.com/a/1905498) deleting the related Taggings first,

@tag = Tag.find(params[:id])
Tagging.where(tag_id: @tag.id).each{ |t| t.destroy }
@tag.destroy

but this is not even hinted at in the text, and ActiveRecord::QueryMethods#where hasn't been introduced at this stage.

Could one of the authors please give a Best Practice for deleting a tag without errors, and amend the tutorial text if necessary?