[英]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.