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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/ruby/271684.html

猜你在找的Ruby相关文章