简体   繁体   English

Rails没有匹配的路线[DELETE]

[英]Rails no routes match [DELETE]

So I'm trying to delete something in my database by making a fetch request in ReactJS , this will be hanled by Rails API Mode . 因此,我试图通过在ReactJS发出获取请求来删除数据库中的ReactJS ,这将由Rails API Mode

So this is how I fetch it in my front-end : 所以这就是我在前端获取它的方式:

removeParticipationToContribution = id_request => {
        fetch(
            `http://localhost:3000/participants?id_request=${id_request}&user_id=${localStorage.getItem(
                'email'
            )}`,
            {
                method: 'DELETE',
                headers: {
                    'Content-Type': 'application/json',
                    'X-User-Email': localStorage.getItem('email'),
                    'X-User-Token': localStorage.getItem('token')
                }
            }
        )
    }

Here is the line of this route : resources :participants, only: [:create, :show, :destroy] 这是这条路线的线路: resources :participants, only: [:create, :show, :destroy]

The problem here is that when I trigger this function, I get this error : ActionController::RoutingError (No route matches [DELETE] "/participants"): 这里的问题是,当我触发此函数时,出现以下错误: ActionController::RoutingError (No route matches [DELETE] "/participants"):

Here is my controller : 这是我的controller

def destroy
        participant = Participant.where(id_request: participant_params[:id_request], user_id: participant_params[:user_id]).first
        puts "---------------"
        puts params.inspect
        puts participant_params[:user_id]
    end

    private def participant_params
        params.permit(:id_request, :user_id)
    end

But it seems like he don't get to that controller . 但是看来他并没有找到那个controller

I tried to do http://localhost:3000/participants/destroy?id_request=... but that gave me a bunch of unpermitted params while those 2 params are permitted. 我尝试做http://localhost:3000/participants/destroy?id_request=...但这给了我一堆unpermitted params而那两个参数是允许的。

The DELETE route (which you can see by running rake routes | grep participants ) is expecting a route in the format: DELETE路由(可以通过运行rake routes | grep participants看到),期望使用以下格式的路由:

DELETE /participants/:id(.:format) participants#destroy

However, you're trying to access the route: 但是,您正在尝试访问该路线:

http://localhost:3000/participants?id_request=(...)&user_id=(...)

This is not the same route. 这是不同的路线。 The id needs to be a route param, not a query param. id必须是路由参数,而不是查询参数。

If this isn't the route format you'd like, then you can define it manually (ie don't just follow the standard convention that rails generated on your behalf). 如果这不是您想要的路线格式,那么您可以手动定义它(即,不要仅仅遵循Rails代表您生成的标准约定)。

For example , you may choose to use: 例如 ,您可以选择使用:

delete 'participants/:id_request/:user_id(.:format)', :to => 'participants#destroy'

Or: 要么:

delete 'participants(.:format)', :to => 'participants#destroy'

(...and then fail in the controller if a required param is missing) (...如果缺少必需的参数,然后在控制器中失败)

But however you choose to do it, the route structure needs to match how you're calling it. 但是,无论您选择执行此操作,路由结构都必须与您的调用方式相匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM