Ruby on rails 4 CanCanCan在异常上抛出常规Rails错误,而不是像我指定的那样抛出flash消息
我正在使用CanCanCan,设计和角色化 我的Ruby on rails 4 CanCanCan在异常上抛出常规Rails错误,而不是像我指定的那样抛出flash消息,ruby-on-rails-4,devise,cancan,rolify,cancancan,Ruby On Rails 4,Devise,Cancan,Rolify,Cancancan,我正在使用CanCanCan,设计和角色化 我的ApplicationController如下所示: class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :except
ApplicationController
如下所示:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :new_post
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, :alert => exception.message
end
def new_post
@post = Post.new
end
end
authenticate :user, lambda { |u| u.has_role? :admin or :editor } do
get 'newsroom', to: 'newsroom#index', as: "newsroom"
get 'newsroom/published', to: 'newsroom#published'
get 'newsroom/unpublished', to: 'newsroom#unpublished'
end
# truncated for brevity
root to: "posts#index"
这是我的能力.rb
,与此相关:
elsif user.has_role? :member
can :read, :all
cannot :read, :newsroom
因此,当我以:member
的身份登录,并尝试进入/newsroom
时,我收到以下错误:
NameError at /newsroom
uninitialized constant Newsroom
而不是像我预期的那样,被重定向到带有:警报的根url
不知道这里发生了什么
编辑1
不管它值多少钱,我只有在我将它添加到我的新闻编辑室控制器时才能得到它:
authorize_resource
我在CanCan方面不是很有经验,但我可以看到您正在从CanCan::AccessDenied中解救,而异常是NameError。因此,你不应该指望你的救援模块能起作用
我想说的是,您可能在某个地方拼错了“Newsroom”,或者正在无法访问它的地方访问它。事实证明,问题在于我授权我的新闻室控制器的方式——因为新闻室是一个非restful控制器(即没有与新闻室相关的模型)
我必须将此添加到控制器的顶部:
authorize_resource :class => false
如此处所述:如果您以管理员身份访问/编辑室,是否会引发错误?@Typpex是的,如果我以管理员身份访问/newsroom
是否会引发错误。文档中说,对于异常处理,我们应该从CanCan::AccessDenied
-