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

ruby-on-rails-4 – CanCanCan在异常时引发常规的Rails错误,而不

发布时间:2020-12-16 19:41:24 所属栏目:百科 来源:网络整理
导读:我正在使用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_fro
我正在使用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.因此,你不应该期望你的救援行动.

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

(编辑:李大同)

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

    推荐文章
      热点阅读