我正在使用Rails 5,并尝试使用Pundit实现我的Rails管理面板的授权。因此,我在应用程序控制器中包含了Pundit,并安装了rails_admin_pundit gem,如你可以在我的Gemfile片段中看到:
gem 'devise'
gem 'devise-i18n'
gem 'rails_admin', '~> 1.0'
gem 'rails_admin-i18n'
gem 'rails_admin_tag_list', github: 'kryzhovnik/rails_admin_tag_list'
gem 'pundit'
gem "rails_admin_pundit", :github => "sudosu/rails_admin_pundit"
应用程序策略:
class ApplicationPolicy
attr_reader :current_user, :record
def initialize(current_user, record)
@user = current_user
@record = record
end
def index?
false
end
def show?
scope.where(:id => record.id).exists?
end
def create?
false
end
def new?
create?
end
def update?
false
end
def edit?
update?
end
def destroy?
false
end
def rails_admin?(action)
case action
when :dashboard
@user.admin?
when :index
@user.admin?
when :show
@user.admin?
when :new
@user.admin?
when :edit
@user.admin?
when :destroy
@user.admin?
when :export
@user.admin?
when :history
@user.admin?
when :show_in_app
@user.admin?
else
raise ::Pundit::NotDefinedError, "unable to find policy #{action} for #{record}."
end
end
end
以下是我的rails_admin初始化器的片段:
RailsAdmin.config do |config|
config.authorize_with :pundit
config.current_user_method(&:current_user)
...
end
现在当我加载管理员仪表板(url:"/admin")时,我会收到以下错误:
undefined method `policy' for #RailsAdmin::MainController:0x0055914e2523a0>
我遵循了所有的说明,但我仍然不知道缺少什么。非常感谢您提供任何答案/建议。