加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – 由于’redirect_to root_url’没有传递动作

发布时间:2020-12-17 03:20:35 所属栏目:百科 来源:网络整理
导读:使用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::MainControll
使用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页面上打开的问题的副本,所以如果另一个解决了,我肯定会关闭一个.

解决方法

根据Github用户的反馈,看来路由是name_scoped,因此这是预期的行为.

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

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

该解决方案的功劳归功于https://github.com/sferik/rails_admin/issues/658年的bbenezech

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读