删除方法栏6

问题描述 投票:0回答:2

我制作了一个Web应用程序,允许用户添加编辑和删除文章。我遇到的问题是delete方法不起作用。当我单击要删除的文章旁边的“删除”时,没有任何反应,我也没有收到任何错误。我认为它充当GET方法而不是DELETE方法,但我不知道为什么它不起作用。

任何帮助将不胜感激!

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 %>

单击“删除”时的控制台:

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")
ruby-on-rails http-delete
2个回答
0
投票

这是错误的。

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

应该是...

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

使用文章显示路径,而不是弧线索引路径。


0
投票

我想出了解决此问题的方法。通过更改

<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>

这解决了我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.