我正在使用 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
因此,当我以 身份登录:member
并尝试转到 时/newsroom
,我收到此错误:
NameError at /newsroom
uninitialized constant Newsroom
而不是像我所期望root_url
的那样被重定向到。:alert
不知道这里发生了什么。
编辑 1
对于它的价值,我只有在将它添加到我的时才会得到这个NewsroomController
:
authorize_resource