需要帮助授权 Pundit 的索引操作
Need help authorizing index action with Pundit
我在我的应用程序上使用 Pundit 和 Devise。我的索引操作在 wikis_controller:
中看起来像这样
def index
@wikis = current_user.wikis
authorize @wikis
end
我试图阻止未登录的用户访问此索引操作,但 @wikis
设置为 undefined
因为没有用户登录。在 return, 不是授权。
我该如何解决这个问题?
您可以针对模型而非对象进行授权。
代码
authorize Wiki
会触发索引吗?没有用户对象的专家策略中的操作。此外,您可以使用范围在另一个级别过滤数据https://github.com/varvet/pundit#scopes。
我在我的应用程序上使用 Pundit 和 Devise。我的索引操作在 wikis_controller:
中看起来像这样 def index
@wikis = current_user.wikis
authorize @wikis
end
我试图阻止未登录的用户访问此索引操作,但 @wikis
设置为 undefined
因为没有用户登录。在 return, 不是授权。
我该如何解决这个问题?
您可以针对模型而非对象进行授权。
代码
authorize Wiki
会触发索引吗?没有用户对象的专家策略中的操作。此外,您可以使用范围在另一个级别过滤数据https://github.com/varvet/pundit#scopes。