Rails post 删除路径被分配给 GET 方法而不是 DELETE

Rails post delete path is assigned to GET method instead of DELETE

提问人:Tomax47 提问时间:8/30/2023 最后编辑:Tomax47 更新时间:9/1/2023 访问量:58

问:

在我关联评论和帖子后,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

结束

Ruby-on-Rails 嵌套路由

评论

0赞 mechnicov 8/30/2023
使用 GET 删除资源是个坏主意
0赞 Tomax47 8/30/2023
感谢您的反馈!!我将“get”更改为“delete”,但它没有帮助,但在 destroy 方法中显示错误。据我了解,这是由我拥有的模型之间的关系引起的,因此我尝试在调用 destroy 方法之前清除帖子中的评论和类似内容,尽管这在post_id的部分导致了另一个 nullViolation 错误。你有没有想过我如何从喜欢和评论中清除帖子,以便我可以删除它?@post.comments.clear
0赞 mechnicov 8/30/2023
你也不需要自定义,因为你有行动delete'/delete_post', too: 'posts#destroy'resourcesdestroy
0赞 mechnicov 8/30/2023
您不需要显式“清除”相关实体,而不需要使用 或 数据库层:dependenthas_manyON DELETE CASCADE

答:

0赞 smathy 8/30/2023 #1

我不确定我是否理解了这一点,您正在输入并想知道为什么要创建 GET 路由get "/a/path", to: "controller#action"

你要踢自己:

delete '/post_delete', to: 'posts#destroy'

但这里真正的问题是为什么你不使用删除路由?然后你就用:resources :posts

button_to 'Delete', @post, method: :delete

评论

0赞 Tomax47 8/30/2023
好吧,我是 rails 的新手,所以认为使用 get 进行后删除是可以的,因为我将它用于用户sign_out销毁会话,所以感谢您提及它!我尝试将其更改为删除,使用删除路由,它不起作用。但相反,开始显示以下错误:resources :postsundefined 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
1赞 Les Nightingill 8/30/2023
它正在尝试调用一个数组。这是您在控制器的销毁方法中拥有的 b/c......它应该是.仔细看看区别。删除方括号 。destroy@post = current_user.posts.find([params[:id]])@post = current_user.posts.find(params[:id])[]
0赞 smathy 8/30/2023
哇,好眼睛@LesNightingill——我还以为你在那儿呆了一秒钟呢:)
0赞 surya narayanan 8/30/2023 #2

问题是您的按钮正在正确触发请求,我认为问题出在销毁期间调用的 before 操作上。correct_user

可能的原因是您的应用程序收到删除帖子的请求,操作开始执行并面临错误。因此,将停止进一步的操作,即不会将请求传递给销毁操作,并且用户将被重定向到请求的来源位置。correct_user

我通过重新创建场景在上面发现了这一点,您可以通过删除销毁方法的 before 操作或在 before_action 中修复问题来确认这一点。correct_user