由于“ redirect_to root_url”未通过操作而导致的RoutingError [英] RoutingError resulting from 'redirect_to root_url' not passing action
问题描述
通过标准安装的Rails_Admin(使用Devise进行身份验证和CanCan进行授权),以 http:// localhost:3000 / admin 的身份访问非管理员用户会生成以下服务器日志:
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>'
直到最后的一切部分似乎还可以。据我所知,CanCan可以正确地挽救异常并尝试通过以下代码重定向到root_url:
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
但是由于某种原因,在重定向到root_url时,CanCan只是尝试击中
But for some reason, in redirecting to root_url, CanCan is only attempting to hit
{:controller=>"gyms"}
而不是
{:controller=>"gyms", :action=>"index"}
这可能是CanCan的问题吗?还是在文档中错过了redirect_to或root_url的某些特定方面?
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?
注意:这是我在CanCan的github页面上打开的一个问题的重复,所以我
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.
推荐答案
根据Github用户的反馈,看来路线可以解决
Based on feedback from users at Github, it appears that routes are being name_scoped and so this is expected behavior.
正确的解决方法是从main_app调用root_url,如下所示:
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
该解决方案的信用位于> https://github.com/sferik/rails_admin/issues/658
Credit for the solution goes to bbenezech at https://github.com/sferik/rails_admin/issues/658
这篇关于由于“ redirect_to root_url”未通过操作而导致的RoutingError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!