红色 Rails 屏幕而不是闪烁消息:CanCan::AccessDenied in RailsAdmin::MainController#dashboard
Red Rails screen instead of flash message: CanCan::AccessDenied in RailsAdmin::MainController#dashboard
我使用 Devise、CanCanCan 和 rails_admin。有了可访问性,一切都很好,但我没有收到一条闪现消息,也没有重定向到 root_path 并得到下面的屏幕。关于如何获取 Flash 消息而不是这个的任何想法和提示?其他闪现消息、警报和通知均显示正常。非常感谢。
我的 ApplicationController 是:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user!
def after_sign_in_path_for(resource)
stored_location_for(resource) || welcome_path_url
end
rescue_from CanCan::AccessDenied do |exception|
respond_to do |format|
format.json { head :forbidden, content_type: 'text/html' }
format.html { redirect_to main_app.root_url, notice: exception.message }
format.js { head :forbidden, content_type: 'text/html' }
end
end
end
rails_admin.rb
RailsAdmin.config do |config|
### Popular gems integration
## == Devise ==
config.authenticate_with do
warden.authenticate! scope: :user
end
config.current_user_method(&:current_user)
## == CancanCan ==
# config.authorize_with :cancancan
config.authorize_with do
redirect_to main_app.root_path unless current_user.superuser ==true
end
<....>
end
ability.rb
class Ability
include CanCan::Ability
def initialize(user)
# Define abilities for the passed in user here. For example:
#
user ||= User.new # guest user (not logged in)
if user.superuser?
can :manage, :all
cannot :access, :rails_admin
cannot :manage, :dashboard
end
if user.office?
cannot :access, :rails_admin
cannot :manage, :dashboard
can :read, :all
end
end
end
编辑:
工作答案已标记。
另外我应该像这样定义状态变量:
def status
@user_role ||= User.new(read_attribute(:user_role))
end
在我的 User
模型中。
似乎 RailsAdmin::MainController
不是从您在应用程序上定义的 ApplicationController 派生的,以捕获在其子类上引发的异常。
您需要显式添加配置以使用另一个父控制器,如下所示。请查找相关 gem 文档 here:
# in config/initializers/rails_admin.rb
config.parent_controller = 'ApplicationController'
堆栈溢出答案也可能对您有所帮助。
我使用 Devise、CanCanCan 和 rails_admin。有了可访问性,一切都很好,但我没有收到一条闪现消息,也没有重定向到 root_path 并得到下面的屏幕。关于如何获取 Flash 消息而不是这个的任何想法和提示?其他闪现消息、警报和通知均显示正常。非常感谢。
我的 ApplicationController 是:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user!
def after_sign_in_path_for(resource)
stored_location_for(resource) || welcome_path_url
end
rescue_from CanCan::AccessDenied do |exception|
respond_to do |format|
format.json { head :forbidden, content_type: 'text/html' }
format.html { redirect_to main_app.root_url, notice: exception.message }
format.js { head :forbidden, content_type: 'text/html' }
end
end
end
rails_admin.rb
RailsAdmin.config do |config|
### Popular gems integration
## == Devise ==
config.authenticate_with do
warden.authenticate! scope: :user
end
config.current_user_method(&:current_user)
## == CancanCan ==
# config.authorize_with :cancancan
config.authorize_with do
redirect_to main_app.root_path unless current_user.superuser ==true
end
<....>
end
ability.rb
class Ability
include CanCan::Ability
def initialize(user)
# Define abilities for the passed in user here. For example:
#
user ||= User.new # guest user (not logged in)
if user.superuser?
can :manage, :all
cannot :access, :rails_admin
cannot :manage, :dashboard
end
if user.office?
cannot :access, :rails_admin
cannot :manage, :dashboard
can :read, :all
end
end
end
编辑: 工作答案已标记。 另外我应该像这样定义状态变量:
def status
@user_role ||= User.new(read_attribute(:user_role))
end
在我的 User
模型中。
似乎 RailsAdmin::MainController
不是从您在应用程序上定义的 ApplicationController 派生的,以捕获在其子类上引发的异常。
您需要显式添加配置以使用另一个父控制器,如下所示。请查找相关 gem 文档 here:
# in config/initializers/rails_admin.rb
config.parent_controller = 'ApplicationController'