문제

Will paginate (3.0.3) doesn't work with inherited resources (1.3.1). In my controller:

protected
def collection
  @posts ||= end_of_association_chain.paginate(:page => params[:page])
end

I added require 'will_paginate/array' in my initializer, but this doesn't fix the problem. How can i get working will paginate and inherited resources together? In my views i get error

undefined method `total_pages' for #<ActiveRecord::Relation:0x00000004312e38>
도움이 되었습니까?

해결책

This is kind of a long shot, but I ran into this problem and it turns out it was CanCan, another gem I am using. This was one of the first things I stumbled on, so I figured it may help someone one day.

Check out: https://github.com/ryanb/cancan/wiki/Inherited-Resources

The call to load_and_authorize_resource loads the collection so the right side of the conditional assignment never gets run in the collection method. The fix that is described in the link above is to skip authorization for collection actions, and do the check explicitly in the collection method.

skip_load_and_authorize_resource :only => :index

protected

def collection
  @posts ||= end_of_association_chain.accessible_by(current_ability).paginate(:page => params[:page])
end

I hope that helps someone.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top