Rails DELETE 路由无法正常工作/未调用控制器功能

Rails DELETE route not working properly / not calling the controller function

我正在使用 React 和 Rails API。为了从 React 发出请求,我使用的是 Axios 库,代码如下所示:

const _deleteIssue = () => {
        axios.delete(`${ROOT_API}/v1/issue/delete/${props.issue.id}`, {
            headers: {
                'Authorization': `Bearer ${authToken}`
            }
        }).then(res => {
            props.updateProjectData()
        }).catch(err => {
            console.log(err)
        })
    }

为了处理这个请求,我为其设置了一条路由,如下所示:

Rails.application.routes.draw do
  concern :base_api do
    # other routes
    post 'issues/create', to: 'issues#create'
    delete 'issue/delete/:issue_id', to: 'issues#delete_issue'
  end

  namespace :v1 do
    concerns :base_api
  end
end

从上面的代码片段可以看出,路由设置为调用 issues 控制器内部的 delete_issue 函数。我已经创建了那个函数,它看起来像这样:

class V1::IssuesController < ApplicationController

    # other functions

    def delete_issue     
        Issue.find_by(id: params[:issue_id]).delete
        render json: {}, status: 200
    end
end

我只是想 find 这个问题的 id 是作为参数从 Axios 删除请求传递的。

它应该删除它并且 return 状态代码为 200 时没有任何内容。相反发生的是,在我的 "Network" 浏览器(Firefox)开发工具内的选项卡中显示200 OK 使用 OPTIONS 方法请求。没有发送 DELETE 方法或任何东西。

我什至试图从 IssuesController 中注释掉 delete_issue 函数并且没有 404 路由错误。结果是一样的。我无法找出它有什么问题,尽管它可能是一个我看不到的非常明显的错误。

注意我用的是Rails6.

你好像没有配置rack-cors。只需将其添加到您的 cors.rb 文件中:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins 'localhost:3001' # or you react app domain

    resource '*',
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end