Ruby on rails Pundit-管理员和用户的索引方法
所以,我想用宝石专家。我只是想弄清楚如何为用户和管理员提供索引视图。我想为管理员呈现所有结果,只为用户呈现相关帖子。我在github上搜索过,但没有找到任何运气。我必须在我的策略和控制器中添加什么 原始代码 控制器Ruby on rails Pundit-管理员和用户的索引方法,ruby-on-rails,ruby,ruby-on-rails-4,pundit,Ruby On Rails,Ruby,Ruby On Rails 4,Pundit,所以,我想用宝石专家。我只是想弄清楚如何为用户和管理员提供索引视图。我想为管理员呈现所有结果,只为用户呈现相关帖子。我在github上搜索过,但没有找到任何运气。我必须在我的策略和控制器中添加什么 原始代码 控制器 class PostsController < ApplicationController before_filter :load_user before_filter :authenticate_user! after_action :verify_authori
class PostsController < ApplicationController
before_filter :load_user
before_filter :authenticate_user!
after_action :verify_authorized
def index
@posts = policy_scope(Post).order('title').page(params[:page]).per(25)
authorize User
end
private
def load_user
@user = User.find_by_id(params[:user_id])
end
end
def index
@posts = policy_scope(Post).order('title').page(params[:page]).per(25)
authorize User
end
控制器
class PostsController < ApplicationController
before_filter :load_user
before_filter :authenticate_user!
after_action :verify_authorized
def index
@posts = policy_scope(Post).order('title').page(params[:page]).per(25)
authorize User
end
private
def load_user
@user = User.find_by_id(params[:user_id])
end
end
def index
@posts = policy_scope(Post).order('title').page(params[:page]).per(25)
authorize User
end
控制器
class PostsController < ApplicationController
before_filter :load_user
before_filter :authenticate_user!
after_action :verify_authorized
def index
@posts = policy_scope(Post).order('title').page(params[:page]).per(25)
authorize User
end
private
def load_user
@user = User.find_by_id(params[:user_id])
end
end
def index
@posts = policy_scope(Post).order('title').page(params[:page]).per(25)
authorize User
end
控制器
class PostsController < ApplicationController
before_filter :load_user
before_filter :authenticate_user!
after_action :verify_authorized
def index
@posts = policy_scope(Post).order('title').page(params[:page]).per(25)
authorize User
end
private
def load_user
@user = User.find_by_id(params[:user_id])
end
end
def index
@posts = policy_scope(Post).order('title').page(params[:page]).per(25)
authorize User
end
class PostsController
您要查找的是一个范围:
class PostsPolicy
class Scope
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve
if user.admin?
scope.all
else
scope.where(user: user)
end
end
end
end
然后在你的控制器里
class PostsController < ApplicationController
before_filter :load_user
before_filter :authenticate_user!
after_action :verify_authorized
def index
@posts = policy_scope(Post).order('title').page(params[:page]).per(25)
authorize User
end
private
def load_user
@user = User.find_by_id(params[:user_id])
end
end
def index
@posts = policy_scope(Post).order('title').page(params[:page]).per(25)
authorize User
end
编辑
作为补充说明,授权用户
从长远来看可能不会很好地为您服务。实际上,您正在创建一个索引策略,该策略需要为每个索引提供服务。如果要授权对索引页的可见性,仍然可以在策略中执行以下操作:
def index?
user.admin? || user.posts.count > 0
end
假设建立了该关系,那么您将在您的策略范围之前在索引控制器中调用
authorize Post
。谢谢!对于试图访问索引Pundit::NotAuthorizedError的用户(不是管理员),我有以下错误-不允许索引?此用户:
pundit(0.3.0)lib/pundit.rb:74:in``authorize'app/controllers/posts\u controller.rb:15:in
index'`棒极了!进步!我假设该用户不是管理员和/或没有任何帖子?管理员-否,有帖子-是。我是否应该改变我的范围,允许用户查看他们自己的帖子,管理员查看所有内容?(愚蠢的问题,但我认为已经是这样了?)让我们后退一步:首先,发生此错误是因为Pundit允许您在授权失败的情况下自由地做任何您想做的事情。这将捕获错误并重定向到您希望它去的任何地方:您的作用域唯一要做的就是缩减集合。它实际上不会阻止某人访问该页面。阻止您访问页面的是用户策略中的index?
方法。我猜是类似于user.admin?
您是否按照建议调用授权帖子?您说您更新了问题,但问题仍然会调用authorize User
。authorize
方法根据传递的参数确定要加载的策略,因此authorize User
将查找用户策略,这是您希望在userscocontroller
中执行的操作。您想授权Post
,这将加载您的Post策略。默认情况下,策略方法始终以用户的身份访问当前用户
。