我有一个路由,我正在发出 DELETE
请求:
user_authorization_path(@user, authorization)
它很好地击中了我的控制器,控制器删除了资源,然后发出重定向:
redirect_to edit_user_path(params[:user_id])
结果是重定向时出现路由错误:
ActionController::RoutingError (No route matches [DELETE] "/users/1/edit")
我可以在日志中看到Rails直到重定向之前都做得很对,但问题在于它试图发出另一个DELETE
而不是GET
请求:
我能看到日志显示Rails在重定向前的行为都是正确的,但重定向时它尝试发出一个DELETE
请求而不是GET
请求:
Started DELETE "/users/1/authorizations/12"...
...
Redirected to http://localhost:3000/users/1/edit
Completed 302 Found in 8ms (ActiveRecord: 0.2ms)
Started DELETE "/users/1/edit"...
ActionController::RoutingError (No route matches [DELETE] "/users/1/edit")
Chrome 调试器显示了初始请求:
Request URL:http://localhost:3000/users/1/authorizations/12
Request Method:DELETE
Status Code:302 Found
接着重定向:
Request URL:http://localhost:3000/users/1/edit
Request Method:GET
Status Code:404 Not Found
看起来浏览器正确地跟随了重定向,但Rails忽略了重定向调用上的GET
,而是使用DELETE
,导致出现404(因为该资源不支持DELETE
- 这本身就是错误的)。
如果我只是在重定向后的URL上执行'GET',那么它可以正常运行。
我对Rails的删除后重定向漏掉了什么?谢谢。