[Rails 6] destroy using the resources method

Introduction

I think the resources method is indispensable for RESTful development ~ (I just want to say)

I learned resources because I thought it was difficult to understand what kind of path was actually taken without seeing the actual contents.

This time I will focus on the destroy that I happen to be implementing! Please implement the delete button!

Check the routing of resources method

routes.rb


Rails.application.routes.draw do
  resources :tweets :only => [:destroy]
end

Normally, I don't think it's easy to set only destroy,

In the terminal, run rake routes to see the routes!

tweets  DELETE /tweets/:id(.:format)  tweets#destroy  

The path that can be set with this is as follows

URL action HTTP method Named path(_path) Named path(_url) Corresponding path
/tweets/:id(.:format) destroy DELETE tweet_path(id) tweet_url(id) /tweets/:id

Delete button implementation example

  <%= link_to "Delete", tweet_path(○○ variable name.id), method: :delete , data: { confirm: "Deleteしますか?"} %>

I think the variable name depends on the time and the case! (In short, it's OK if you can get the id of the record you want to delete)

that's all~

Recommended Posts

[Rails 6] destroy using the resources method
[Rails] I tried using the button_to method for the first time
rails method
[Ruby on Rails] Use the resources method to automatically create routes.
Try using the Rails API (zip code)
[Rails] How to use the map method
Get the error message using the any? method
Rails delegate method
About the method
[Order method] Set the order of data in Rails
Output about the method # 2
[Ruby] Cut out a string using the slice method
[Rails] I learned about the difference between resources and resources
[Rails] devise helper method
About the length method
Rails6: Input the initial data of ActionText using seed
About the authenticate method.
Training using each_with_index method
[Rails] devise introduction method
I want to call the main method using reflection
About the map method
About the ancestors method
Are you using the default method of the interface properly?
About the to_s method.
Try using each_with_index method
Master the [Rails] scope!
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
Try using the query attribute of Ruby on Rails
[Ruby] Count an even number in an array using the even? Method
[Enum] Let's improve the readability of data by using rails enum
[Rails] Register by attribute of the same model using Devise
[Rails] Create an echo bot using the LINE Messaging API.
Add an icon to the header link using Rails fontawesome
Search function using [rails] ransack
Note the StringUtils # containsAny method
What is the pluck method?
Output about the method Part 1
SNS authentication using Rails google
[Rails] Save images using carrierwave
Rails: Difference between resources and resources
[Rails] require method and permit method
Rails "render method" and "redirect method"
11.1 AccountActivations Resources: Rails Tutorial Notes-Chapter 11
[Rails] Japanese localization using rails-i18n
Understand the helper method form_with
Rails ~ Understanding the message function ~
[Rails] Test code using Rspec
Organize Rails routing using draw
Ajax bookmark function using Rails
Error when using rails capybara
Consideration about the times method
SwiftUI-Display the map using MapKit
[Rails] Try using Faraday middleware
Detailed tips when using Rails
[Rails] Delete the migration file
[Rails 6] Star-shaped review using Raty.js
About Rails scraping method Mechanize
What is the initialize method?
I tried to understand how the rails method "redirect_to" is defined
I tried to understand how the rails method "link_to" is defined
[Rails] Don't use the select method just to narrow down the columns!