Rails中destroy方法失效:点击删除链接仅跳转未删帖子
问题:Destroy方法无法正常删除帖子
站点中创建了删除链接,但点击后仅跳转到根路径,目标帖子并未被删除。在Rails控制台中手动删除帖子可正常生效。
相关代码
PostsController代码
class PostsController < ApplicationController def index @posts = Post.all end def show @post = Post.find(params[:id]) end def new @post = Post.new end def create @post = Post.new(post_params) if @post.save redirect_to @post else render :new, status: :unprocessable_entity end end def edit @post = Post.find(params[:id]) end def update @post = Post.find(params[:id]) if @post.update(post_params) redirect_to @post else render :edit, status: :unprocessable_entity end end def destroy @post = Post.find(params[:id]) @post.destroy redirect_to root_path, status: :see_other end private def post_params params.require(:post).permit(:title, :description, :image, :price) end end
视图posts/show.html.erb中的删除链接代码
<%= link_to 'Delete', root_path, method: :delete, data: { confirm: 'Are you sure?' } %>
使用版本
- Rails 7.0.4
- Ruby 3.1.2
点击删除链接后的服务器日志
Started GET "/posts/3" for ::1 at 2022-10-09 23:59:02 -0400 ActiveRecord::SchemaMigration Pluck (0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC Processing by PostsController#show as HTML Parameters: {"id"=>"3"} Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]] ↳ app/controllers/posts_controller.rb:7:in `show' Rendering layout layouts/application.html.erb Rendering posts/show.html.erb within layouts/application ActiveStorage::Attachment Load (0.1ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 3], ["record_type", "Post"], ["name", "image"], ["LIMIT", 1]] ↳ app/views/posts/show.html.erb:11 ActiveStorage::Blob Load (0.1ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/views/posts/show.html.erb:12 Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? [["post_id", 3]] ↳ app/views/posts/show.html.erb:23 Rendered collection of templates [0 times] (Duration: 0.0ms | Allocations: 35) Rendered comments/_form.html.erb (Duration: 31.1ms | Allocations: 6334) Rendered posts/show.html.erb within layouts/application (Duration: 142.9ms | Allocations: 31474) Rendered layout layouts/application.html.erb (Duration: 300.1ms | Allocations: 53293) Completed 200 OK in 402ms (Views: 312.5ms | ActiveRecord: 2.1ms | Allocations: 66385) Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDVG9JYTJWNVNTSWhiR28xZVRKamJucHhZM0pxZFhaak9YZGxZelY0ZVdwbGREWnlNZ1k2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpWldsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SW1sc1h6RTFPRGg0VGk0ek1qSTNNRGc0TlRZMVh6SmlkWGd1Y0c1bklqc2dabWxzWlc1aGJXVXFQVlZVUmkwNEp5ZHBiRjh4TlRnNGVFNHVNekl5TnpBNE9EVTJOVjh5WW5WNExuQnVad1k3QmxRNkVXTnZiblJsYm5SZmRIbHdaVWtpRG1sdFlXZGxMM0J1WndZN0JsUTZFWE5sY25acFkyVmZibUZ0WlRvS2JHOWpZV3c9IiwiZXhwIjoiMjAyMi0xMC0xMFQwNDowMTo0My45NDVaIiwicHVyIjoiYmxvYl9rZXkifX0=--00b620d927983a0cba2300d10b1e2234b9306a84/il_1588xN.3227088565_2bux.png" for ::1 at 2022-10-09 23:59:03 -0400 Processing by ActiveStorage::DiskController#show as PNG Parameters: {"encoded_key"=>"[FILTERED]", "filename"=>"il_1588xN.3227088565_2bux"} Completed 304 Not Modified in 7ms (ActiveRecord: 0.0ms | Allocations: 559)
问题分析与解决方案
从服务器日志能看到,点击删除链接后触发的是GET "/posts/3"请求(对应PostsController#show动作),而非预期的DELETE "/posts/3"请求(对应destroy动作),这是核心问题。
1. 修正删除链接的目标路径
当前链接指向root_path完全错误,应该指向当前帖子的destroy路由。修改视图中的链接代码:
<%= link_to 'Delete', post_path(@post), method: :delete, data: { confirm: 'Are you sure?' } %>
修改后链接会指向/posts/:id,配合method: :delete触发destroy动作。
2. 确认Turbo加载状态(Rails 7默认依赖)
Rails 7默认用Turbo处理非GET请求(如DELETE、PUT等),如果页面没正确加载Turbo,method: :delete会失效,退化为GET请求。检查app/views/layouts/application.html.erb是否包含:
<%= javascript_importmap_tags %>
同时确认config/importmap.rb中有Turbo的引入:
pin "turbo", to: "turbo.min.js", preload: true
确保Turbo正常加载,才能正确发送DELETE请求。
内容的提问来源于stack exchange,提问作者Jillian Hoenig
相关产品推荐
相关产品推荐

