微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.因此,你不应该期望你的救援行动.

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

原文地址:https://www.jb51.cc/ruby/271684.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐