简体   繁体   English

为什么存在路由可以视为没有路由匹配(RoutingError)?

[英]Why does the route exist can be regarded as no route matches(RoutingError)?

The rails server says Rails服务器说

Started POST "/user/1/follow" 

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

But using rake routes 但是使用rake routes

         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

Based on the information above I have no idea why the route exist can be regarded as no route matches(RoutingError). 基于上面的信息,我不知道为什么存在路由可以被视为没有路由匹配(RoutingError)。 It seems that the route does exist. 该路线似乎确实存在。

Below is other information maybe relevant to this issue. 以下是其他可能与此问题相关的信息。 I'm using jquery to trigger the route by 我正在使用jquery通过以下方式触发路线

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

        });

And users#follow is defined as 并且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

You've defined your route as 您已将路线定义为

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

But in the jQuery snippet, you're using /user/... instead of /users/... 但是在jQuery代码段中,您使用的是/user/...而不是/users/...

The path of your route is different in the jquery post, the correct route 'users/1/follow' , a super tip I use in my work, use the gem js-routes that provides the path of their routes to be accessed in JavaScripts. 您的路线路径在jquery帖子中有所不同,正确的路线'users/1/follow' (我在工作中使用的超级技巧)使用gem js-routes提供了可在JavaScript中访问的路线路径。

Hope this helps :) 希望这可以帮助 :)

暂无
暂无

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

相关问题 RoutingError(没有匹配的路由[OPTIONS] - RoutingError (No route matches [OPTIONS] RoutingError没有与[GET]“ /missing.png”匹配的路由 - RoutingError No route matches [GET] “/missing.png” ActionController :: RoutingError(没有路由与[GET]匹配” / assets / javascripts - ActionController::RoutingError (No route matches [GET] "/assets/javascripts 为什么我得到这个 ActionController::RoutingError (没有路由匹配 [GET] “/users/assets/application.js”) - Why I am getting this ActionController::RoutingError (No route matches [GET] “/users/assets/application.js”) 如何找到导致ActionController :: RoutingError的原因,没有路由匹配错误? - How can I find what is causing the ActionController::RoutingError No route matches error? Rails CORS:ActionController::RoutingError(没有路由匹配 [OPTIONS] "/batches"): - Rails CORS: ActionController::RoutingError (No route matches [OPTIONS] "/batches"): ActionController :: RoutingError(没有路由与[GET]“ / javascripts / application.js”匹配) - ActionController::RoutingError (No route matches [GET] “/javascripts/application.js”) ActionController :: RoutingError(没有路由与[GET] controller_name / fusioncharts.widgets.js匹配 - ActionController::RoutingError (No route matches [GET] controller_name/fusioncharts.widgets.js Ruby on Rails:PDF.JS ActionController :: RoutingError(没有路由与[GET]“ / pdf js / web / viewer.html”匹配): - Ruby on Rails: PDF.JS ActionController::RoutingError (No route matches [GET] “/pdf js/web/viewer.html”): ActionController :: RoutingError(没有路由匹配[GET]“/members/js/bootstrap.min.js"):(Missing资产文件) - ActionController::RoutingError (No route matches [GET] “/members/js/bootstrap.min.js”):(Missing Asset files)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM