提问人:Tomax47 提问时间:8/30/2023 最后编辑:Tomax47 更新时间:9/1/2023 访问量:58
Rails post 删除路径被分配给 GET 方法而不是 DELETE
Rails post delete path is assigned to GET method instead of DELETE
问:
在我关联评论和帖子后,post_delete_path开始发送 GET 请求而不是 Delete 请求,显示以下错误: <没有路由匹配 [DELETE] “/delete_post.3”>
routes.erb 文件:
Rails.application.routes.draw do
#get 'posts/index'
resources :posts do
resources :comments, only: [:destroy, :create]
end
delete'/delete_post', to: 'posts#destroy'
resources :likes, only: [:destroy, :create]
resources :sessions, only: [:create, :destroy]
get 'sessions/new'
get 'sing_in', to: 'sessions#new'
get 'sign_out', to: 'sessions#destroy'
resources :users, only: [:create, :destroy]
get 'users/new'
get '/register', to: 'users#new'
root 'posts#index'
end
职位总监 :
class PostsController < ApplicationController
before_action :ensure_user
before_action :correct_user, only: [:edit, :update, :destroy]
def index
@posts = Post.all.order("created_at DESC")
end
def show
@post = Post.find(params[:id])
end
def new
@post = current_user.posts.new
end
def create
@post = current_user.posts.new(post_params)
if @post.save
redirect_to post_url(@post)
else
render 'new', notice: "Couldn't create the post!"
end
end
def edit
@post = set_post
end
def update
@post = current_user.posts.find(params[:id])
if @post.update(post_params)
redirect_to post_url(@post)
else
redirect_to post_url(@post), notice: "Couldn't update the post!"
end
end
def destroy
@post = current_user.posts.find([params[:id]])
if @post.destroy
redirect_to posts_path, notice: "Post has been deleted!"
else
redirect_to post_url(@post), notice: "Couldn't delete the post!"
end
end
private
def correct_user
@post = current_user.posts.find_by(id: params[:id])
redirect_to root_path, notice: "You are not authorized to edit this post!" if @post.nil?
end
def set_post
return Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :content)
end
end
在帖子视图中显示文件中的删除按钮:
<%= button_to 'Delete', delete_post_path(@post), method: :delete %>
删除带有 DELETE 请求的帖子的路由未显示,即使在路由文件中指定后也是如此:在此处输入图像描述
我尝试在帖子资源下的路由中指定删除路径,但它使用 GET 方法而不是 DELETE 显示新路径。
resources :posts do
resources :comments, only: [:destroy, :create]
delete'/post_delete', to: 'posts#destroy'
end
resources :posts do
resources :comments, only: [:destroy, :create]
end
delete'/post_delete', to: 'posts#destroy'
此外,我尝试在button_to和link_to之间切换,尽管它没有帮助。
更新:
我发现导致问题的有两件事,首先是路由问题,我用 get 方法而不是 delete 方法定义了一个post_delete路由,尽管我有可以使用的方法,所以我摆脱了它。其次,在删除带有相关评论或点赞的帖子时,我收到 NotNullViolation 错误,因为评论和点赞的post_id不能为空,所以为了克服这个问题,在帖子控制器的 destroy 函数中,我在删除帖子之前删除了与帖子相关的所有评论和点赞: ```
def 销毁
@post = current_user.posts.find(参数[:id])resources :posts
@post.likes.destroy_all
@post.comments.destroy_all
if @post.destroy
redirect_to posts_path, notice: "Post has been successfully deleted!"
else
redirect_to @post
flash[:notice] = "Can't delete the post!"
end
结束
答:
我不确定我是否理解了这一点,您正在输入并想知道为什么要创建 GET 路由?get "/a/path", to: "controller#action"
你要踢自己:
delete '/post_delete', to: 'posts#destroy'
但这里真正的问题是为什么你不使用删除路由?然后你就用:resources :posts
button_to 'Delete', @post, method: :delete
评论
resources :posts
undefined method `destroy' for [#<Post id: 3, title: "Someone posted something ", content: "New content for all Now", created_at: "2023-08-28 14:14:23.367126000 +0000", updated_at: "2023-08-28 14:34:46.268273000 +0000", user_id: 1>]:Array
destroy
@post = current_user.posts.find([params[:id]])
@post = current_user.posts.find(params[:id])
[]
问题是您的按钮正在正确触发请求,我认为问题出在销毁期间调用的 before 操作上。correct_user
可能的原因是您的应用程序收到删除帖子的请求,操作开始执行并面临错误。因此,将停止进一步的操作,即不会将请求传递给销毁操作,并且用户将被重定向到请求的来源位置。correct_user
我通过重新创建场景在上面发现了这一点,您可以通过删除销毁方法的 before 操作或在 before_action 中修复问题来确认这一点。correct_user
评论
@post.comments.clear
delete'/delete_post', too: 'posts#destroy'
resources
destroy
:dependent
has_many
ON DELETE CASCADE