使用 cancan 限制对索引的访问

Restricting access to Index with cancan

我正在使用 cancan 来锁定我创建的应用程序。它工作得很好,但我不太清楚如何以我想要的方式锁定特定模型的索引。

索引如下所示:

    def index
        @incorporations = current_user.incorporations("created_at DESC")
    end

我所追求的很简单:希望索引只能由注册用户访问。我一直在使用 if user_signed_in 语句,但我想知道是否有一种方法可以使用 cancan 来实现一致性。

提前致谢

答案很简单,我从here.

得到的

我需要做的就是将以下内容添加到我的 ablility.rb:

can :index, Incorporation

然后将以下内容添加到有问题的控制器中:

authorize! :index, Ability

就是这样!