[RUBY] [For beginners] How to implement the delete function

Posted content

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. image.png

Controller action

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.

Ruby description

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.

Finally

The delete function this time is very simple, so even beginners may find it easy to get started. Please refer to it!

Recommended Posts

[For beginners] How to implement the delete function
How to add the delete function
[Swift] How to implement the countdown function
[Swift] How to implement the LINE login function
How to implement the breadcrumb function using gretel
[Swift] How to implement the fade-in / out function
How to implement login request processing (Rails / for beginners)
[For Ruby beginners] Explain how to freely delete array elements!
[Rails] How to display error messages for comment function (for beginners)
How to delete the wrong migration file
How to delete the migration file NO FILE
[Ruby] How to use slice for beginners
How to implement TextInputLayout with validation function
[Processing × Java] How to use the function
[For beginners] How to debug in Eclipse
[Behavior confirmed in December 2020] How to implement the alert display function
[For beginners] How to get the Ruby delayed railway line name
[Swift] How to implement the Twitter login function using Firebase UI ②
Implement the star five function using the for statement
[Rails] How to implement unit tests for models
How to delete the database when recreating the application
[Java] (for MacOS) How to set the classpath
[For super beginners] How to use autofocus: true
How to implement the email authentication function at the time of user registration
How to implement Pagination in GraphQL (for ruby)
How to delete / update the list field of OneToMany
[Spring Boot] How to create a project (for beginners)
Rails learning How to implement search function using ActiveModel
How to make a mod for Slay the Spire
How to use GitHub for super beginners (team development)
I want to add a delete function to the comment function
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
How to add ActionText function
[Rails] How to implement scraping
[Java] How to implement multithreading
[Ruby on Rails] How to implement tagging / incremental search function for posts (without gem)
I tried to implement the like function by asynchronous communication
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
SDWebImage: How to clear the cache for a particular UIImageView
[Swift] I tried to implement the function of the vending machine
For Java beginners: List, Map, Iterator / Array ... How to convert?
How to use the form_with method
How to specify validation for time_field
How to install JMeter for Mac
How to find the average angle
How to use the wrapper class
Try to implement iOS14 Widget function
[Swift 5] Implement UI for review function
[Rails] How to implement star rating
[Beginner] How to delete NO FILE
Must-see for beginners! How to manage your Xcode project on Github
[RSpec on Rails] How to write test code for beginners by beginners
I tried to implement the image preview function with Rails / jQuery
[Tips] How to solve problems with XCode and Swift for beginners
How to get the contents of Map using for statement Memorandum
[Rails] How to change the page title of the browser for each page
[Rails] How to put a crown mark on the ranking function
kotlin & Java: How to hide the toolbar only for specific fragments
How to check for the contents of a java fixed-length string
How to use MinIO with the same function as S3 Use docker-compose