위키 목록이 긴 (게시물과 같은) 앱을 만들고 있습니다. 앱에서 Pundit을 사용하고 있습니다. 페이지 매김을 위해 두 개의 보석, will_paginate 및 kaminari을 발견했습니다.스코프 클래스의 레일에서 페이지 매김을 사용하는 방법은 무엇입니까?
이 같은 WikiController는 모습입니다 :
class WikisController < ApplicationController
def index
if current_user == nil
@wikis = Wiki.visible_to_all
else
@wikis = policy_scope(Wiki) #pundit
end
visible_to_all
이 관리자가 아닌 사용자에게 모든 비 개인 위키를 표시하기 위해 만든 범위입니다.
#on Wiki.rb (model)
scope :visible_to_all, -> {where(private: [false, nil])}
어쨌든, 지금은 위키의 과다를하고 페이지를 매기는하고 싶습니다.
class UserController
def index
@users = User.paginate(:page => params[:page], :per_page => 5)
end
미나리는 컨트롤러에서 이렇게 말한다 : will_paginate
관련 컨트롤러에이 일을 말한다 내가 Wikis
범위를 지정하고 있기 때문에 @wikis
될 것입니다 내 경우 @users
에
@users = User.order(:name).page params[:page]
; 그러나 @wikis
은 이미 범위 지정에 사용됩니다. 범위를 유지하고 내 Wiki에 페이지 매기기를 적용하려면 어떻게해야합니까?
편집 : wiki_policy.rb :
def resolve
wikis = []
if user.role == 'admin'
wikis = scope.all # if the user is an admin, show them all the wikis
elsif user.role == 'premium'
all_wikis = scope.all
all_wikis.each do |wiki|
if wiki.private == false || wiki.private == nil || wiki.user == user || wiki.users.include?(user)
wikis << wiki # if the user is premium, only show them public wikis, or that private wikis they created, or private wikis they are a collaborator on
end
end
else # this is the lowly standard user
all_wikis = scope.all
wikis = []
all_wikis.each do |wiki|
if wiki.private == false || wiki.users.include?(user)
wikis << wiki # only show standard users public wikis and private wikis they are a collaborator on
end
end
end
wikis # return the wikis array we've built up
end
(!) 참고 : 다음 학자-범위에 범위는 당신이 뭔가를 할 수 있습니다 당신은 학자 범위에 .visible_to_all 범위를 통합 할 수 있을까요? 동일한 current_user는 Pundit 범위에서 (사용자로 명명 된) 범위에서도 사용할 수 있습니다. – ollpu
나는 그것을 찾을 것이다! 이것은 내가 만든 첫 번째 레일 앱 중 하나이기 때문에 가장 효율적이지는 않습니다. 그러나 그것은 좋은 제안입니다! 나는 되돌아 가서 그것을 리팩토링 할 것이다 :) 고마워! – Iggy