ruby-on-rails-4 – CanCanCan在异常时引发常规的Rails错误,而不是像我指定的Flash消息

我正在使用CanCanCan,Devise& Rolify.

我的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

我的routes.rb如下所示:

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"

这是我的ability.rb相关的:

elsif user.has_role? :member
    can :read,:all
    cannot :read,:newsroom

所以当我以:成员身份登录时,我尝试去/新闻编辑室,我得到这个错误

NameError at /newsroom
uninitialized constant Newsroom

而不是被重定向到root_url与一个:警报像我预期的.

不知道这里发生了什么.

编辑1

对于什么是值得的,只有当我将它添加到我的新闻室控制器中才能得到:

authorize_resource

解决方法

我在CanCan中不是很有经验,但是我可以看到你正在从CanCan :: AccessDenied中拯救,而异常是一个NameError.因此,你不应该期望你的救援行动.

我会说你可能在某个地方拼错“新闻室”,或正在访问它不可用的地方.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...