This time I will post about how to implement the delete function.
Activates the destroy action when you press a button like the one below.

items_controller.rb
class ItemsController < ApplicationController
#abridgement
  def destroy
    if @item.destroy
      redirect_to root_path
    else
      render :show
    end
  end
end
Explanation) When the destroy action is called, the instance variable @item is deleted. If it can be deleted successfully, it will transition to root_path. If it cannot be deleted, it will be handled so that it will transition to the show action.
show.html.haml
.item-show-page__destroy-btn
  = link_to "Delete the item", item_path(@item), method: :delete, class: "item-show-page__destroy-link"
Explanation) Specify @item with link_to, and specify delete for the method.
Now you can delete it by clicking the button.
The delete function this time is very simple, so even beginners may find it easy to get started. Please refer to it!
Recommended Posts