由“redirect_to root_url”导致的路由错误不通过行动

发布于 2024-11-28 15:36:42 字数 1373 浏览 1 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扶醉桌前 2024-12-05 15:36:42

根据 Github 用户的反馈,路由似乎正在 name_scoped 范围内,因此这是预期的行为。

正确的修复方法是从 main_app 调用 root_url,如下所示:

rescue_from CanCan::AccessDenied do |exception|
  redirect_to main_app.root_url, :alert => exception.message
end

解决方案的功劳归于 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:

rescue_from CanCan::AccessDenied do |exception|
  redirect_to main_app.root_url, :alert => exception.message
end

Credit for the solution goes to bbenezech at https://github.com/sferik/rails_admin/issues/658

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文