本文介绍了CanCan和没有模型的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CanCan进行授权。我在/app/config/ability.rb中定义了模型操作用户规则,它可以正常工作。我已经将行 load_and_authorize_resource 添加到我的application_controller中,一切都已完成。

I'm using CanCan for authorization. I define the model-action-user rules in /app/config/ability.rb and it's working fine. I've added the line load_and_authorize_resource to my application_controller, and everything's done.

但是,我也有很多视图以及下面没有模型的控制器。例如,尝试加载统计信息页面会

However, I also have numerous views and controllers that don't have a model underneath. For example, trying to load a statistics page gives

NameError (uninitialized constant Statistic):
  activesupport (3.2.3) lib/active_support/inflector/methods.rb:229:in `block in constantize'
  activesupport (3.2.3) lib/active_support/inflector/methods.rb:228:in `each'
  activesupport (3.2.3) lib/active_support/inflector/methods.rb:228:in `constantize'
  ...

CanCan是否可以通过某种方式使用controller + action而不是model + action?

Is there some way for CanCan to work with the controller+action instead of model+action?

推荐答案

使用 authorize_resource:class => false 在您的控制器中。 CanCan将自动检查控制器名称上的功能(作为符号,以单数形式,例如 StatisticsController :statistic >)

Use authorize_resource :class => false in your controller. CanCan will automatically check for abilities on the name of the controller (as a symbol, singular, eg :statistic for the StatisticsController)

请参见

这篇关于CanCan和没有模型的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 11:32