由“redirect_to root_url”导致的路由错误不通过行动
使用 Devise 进行身份验证并使用 CanCan 进行授权的 Rails_Admin 标准安装,以非管理员身份访问 http://localhost:3000/admin用户生成以下服务器日志:
Started GET "/admin" for 127.0.0.1 at 2011-08-09 22:46:10 -0400
Processing by RailsAdmin::MainController#index as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Completed 404 Not Found in 151ms
ActionController::RoutingError (No route matches {:controller=>"gyms"}):
app/controllers/application_controller.rb:5:in `block in <class:ApplicationController>'
直到最后一部分的一切似乎都正常。据我所知,CanCan 正确地挽救了异常,并尝试通过以下代码重定向到 root_url:
class ApplicationController < ActionController::Base
protect_from_forgery
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, :alert => exception.message
end
end
TopOut::Application.routes.draw do
mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
devise_for :users
resources :gyms
root :to => "gyms#index"
end
但由于某种原因,在重定向到 root_url 时,CanCan 只是尝试命中
{:controller=>"gyms"}
而不是
{:controller=>"gyms", :action=>"index"}
这可能是 CanCan 的问题吗?或者我在文档中错过了redirect_to 或root_url 的某些特定方面?
注意:这是我在 CanCan 的 github 页面上打开的问题的重复项,因此如果另一个问题得到解决,我一定会关闭其中一个问题。
With a standard install of Rails_Admin using Devise for authentication and CanCan for authorization, accessing http://localhost:3000/admin as a non-admin user produces the following server log:
Started GET "/admin" for 127.0.0.1 at 2011-08-09 22:46:10 -0400
Processing by RailsAdmin::MainController#index as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Completed 404 Not Found in 151ms
ActionController::RoutingError (No route matches {:controller=>"gyms"}):
app/controllers/application_controller.rb:5:in `block in <class:ApplicationController>'
Everything up until the last part seems ok. As far as I can tell, CanCan rescues the exception properly and attempts to redirect to root_url via the following code:
class ApplicationController < ActionController::Base
protect_from_forgery
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, :alert => exception.message
end
end
TopOut::Application.routes.draw do
mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
devise_for :users
resources :gyms
root :to => "gyms#index"
end
But for some reason, in redirecting to root_url, CanCan is only attempting to hit
{:controller=>"gyms"}
rather than
{:controller=>"gyms", :action=>"index"}
Is this possibly an issue with CanCan? Or is there some particular facet of redirect_to or root_url which I missed in the docs?
Note: this is a duplicate of an issue I opened on CanCan's github page, so I'll be sure to close one if the other is solved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 Github 用户的反馈,路由似乎正在 name_scoped 范围内,因此这是预期的行为。
正确的修复方法是从 main_app 调用 root_url,如下所示:
解决方案的功劳归于 bbenezech,网址为 https:// github.com/sferik/rails_admin/issues/658
Based on feedback from users at Github, it appears that routes are being name_scoped and so this is expected behavior.
Proper fix is to call root_url from main_app as follows:
Credit for the solution goes to bbenezech at https://github.com/sferik/rails_admin/issues/658