gpt4 book ai didi

javascript - 为什么路由存在会被视为没有路由匹配(RoutingError)?

转载 作者:行者123 更新时间:2023-12-03 12:03:56 28 4
gpt4 key购买 nike

rails 服务器说

Started POST "/user/1/follow" 

ActionController::RoutingError (No route matches [POST] "/user/1/follow"):

但是使用rake 路由

         Prefix Verb   URI Pattern                     Controller#Action
followers_user GET /users/:id/followers(.:format) users#followers
followings_user GET /users/:id/followings(.:format) users#followings
follow_user POST /users/:id/follow(.:format) users#follow

根据上面的信息,我不知道为什么存在的路由可以被视为没有路由匹配(RoutingError)。看来这条路线确实存在。

以下是可能与此问题相关的其他信息。我正在使用 jquery 来触发路由

  $.ajax({
url: '/user/'+userId+'/follow',
type: 'POST',

});

users#follow 定义为

    def follow
if current_user?(@user)
flash[:error] = "You cannot follow yourself"
elsif current_user.following?(@user)
flash[:error] = "You already follow #{@user.name}"
end
if request.xhr?
render status: current_user.follow(@user) ? 200 : 400, nothing: true
end
end

最佳答案

您已将路线定义为

follow_user POST   /users/:id/follow(.:format)     users#follow

但是在 jQuery 代码段中,您使用的是 /user/... 而不是 /users/...

关于javascript - 为什么路由存在会被视为没有路由匹配(RoutingError)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25270539/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com