删除方法rails 6
Delete method rails 6
我制作了一个允许用户添加编辑和删除文章的网络应用程序。我遇到的问题是删除方法不起作用。当我单击要删除的文章旁边的 "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")
这是错误的。
<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>
使用文章显示路径,而不是文章索引路径。
我找到了解决此问题的方法。通过改变
<td><%= link_to 'Delete', article_path(article), method: :delete, data: {confirm: "Are you sure?"} %></td>
至
<td><%= button_to 'Delete', article_path(article), method: :delete, data: {confirm: "Are you sure?"} %></td>
这解决了我的问题。
我制作了一个允许用户添加编辑和删除文章的网络应用程序。我遇到的问题是删除方法不起作用。当我单击要删除的文章旁边的 "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")
这是错误的。
<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>
使用文章显示路径,而不是文章索引路径。
我找到了解决此问题的方法。通过改变
<td><%= link_to 'Delete', article_path(article), method: :delete, data: {confirm: "Are you sure?"} %></td>
至
<td><%= button_to 'Delete', article_path(article), method: :delete, data: {confirm: "Are you sure?"} %></td>
这解决了我的问题。