1

I made a web application that allows the user to add edit and delete an article. The problem I'm having is that the delete method does not work. When I click "Delete" beside the article that i want to get rid off nothing happens, I don't get any errors either. I think its acting as a GET method rather than a DELETE method but I have no idea why it won't work.

Any help will be greatly appreciated!

articles_controller.rb

def destroy
  @article = Article.find(params[:id])
  @article.destroy
  redirect_to articles_path
end

index.html.erb

<tbody>
  <% @articles.each do |article| %>
  <tr>
    <td><%= article.title %></td>
        <td><%= article.description %></td>

        <td><%= link_to 'Show', article_path(article) %></td>
        <td><%= link_to 'Edit', edit_article_path(article) %></td>
        <td><%= link_to 'Delete', articles_path(article), method: :delete, data: {confirm: "Are you sure?"} %></td>

  </tr>
  <% end %>

Console when i click Delete:

Started GET "/articles.5" for ::1 at 2020-03-26 16:45:32 +0000
Processing by ArticlesController#index as
  Rendering articles/index.html.erb within layouts/application
  Article Load (0.4ms)  SELECT "articles".* FROM "articles"
  ↳ app/views/articles/index.html.erb:14
  Rendered articles/index.html.erb within layouts/application (Duration: 4.1ms | Allocations: 1596)
[Webpacker] Everything's up-to-date. Nothing to do
  Rendered layouts/_navigation.html.erb (Duration: 0.3ms | Allocations: 85)
Completed 200 OK in 72ms (Views: 70.3ms | ActiveRecord: 0.4ms | Allocations: 8290)

routes.rb

Rails.application.routes.draw do
  root 'pages#home'
  resources :articles, only: [:show, :index, :new, :create, :edit, :update, :destroy]

end

Application.js

require("@rails/ujs").start()
require jquery
require jquery_ujs
require bootstrap-sprockets
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
Eoin
  • 252
  • 2
  • 15
  • This is a classic Rails issue and you can find numerous solutions, but there could be a few reasons. Likely it involves jquery not being loaded, I would look there first, can you verify all your JS is being loaded on that page? – Rockwell Rice Mar 26 '20 at 16:02
  • What do your routes look like? What does the console look like after you click the delete link? – hashrocket Mar 26 '20 at 16:35
  • @hashrocket I edited this post to show my routes.rb and console when i click delete :) – Eoin Mar 26 '20 at 16:49
  • @RockwellRice I added my code from my applications.js file. I think everything i need is there – Eoin Mar 26 '20 at 16:53
  • Right, but can you confirm they are getting loaded on the page? Inspect the page and see if they are there – Rockwell Rice Mar 26 '20 at 18:01
  • If you insect that link, is the route really `/articles.5` with a `.` and not a `/` ? – Rockwell Rice Mar 26 '20 at 18:04

2 Answers2

13

I figured out a fix for this problem. By changing

<td><%= link_to 'Delete', article_path(article), method: :delete, data: {confirm: "Are you sure?"} %></td>

to

<td><%= button_to 'Delete', article_path(article), method: :delete, data: {confirm: "Are you sure?"} %></td>

This fixed my issue.

Eoin
  • 252
  • 2
  • 15
0

This is wrong.

<td><%= link_to 'Delete', articles_path(article), method: :delete, data: {confirm: "Are you sure?"} %></td>

It should be...

<td><%= link_to 'Delete', article_path(article), method: :delete, data: {confirm: "Are you sure?"} %></td>

Use the article show path, not the arcticles index path.

SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53