2

I'm making a Spree site that has links for changing the number of products per page including a link for All. The links for a number are easy because I can just pass a :per_page param via the query string and helper methods. However, I can't figure out what I need to pass to either tell Kaminari to not paginate or to temporarily disable Kaminari.

I'm happy for a solution either in Spree or in Kaminari. I would prefer a method other than :per_page = 1000 or some similarly large number.

  • Spree 0.70.3
  • Kaminari 0.13.0
Yogh
  • 591
  • 6
  • 17

3 Answers3

1

Could you pass a querystring parameter and then filter it inside your controller action? For example:

def show
  @products = unless params[:show_all]
    Product.page(params[:page]).per(params[:per_page])
  else
    Product.all
  end
end

I know this doesn't give you a solution in either Spree or Kaminari but it might help to work around the issue. I'd love to know if there was another way built in to the library though.

parndt
  • 1,873
  • 11
  • 13
  • 1
    @parndt Testing shows that this basically works, but Spree sets the equivalent of `@products` deep within its bowels so you have to intercept things in a controller decorator and alter the querystring parameter with `params[:per_page] = Product.count if params[:per_page] == 'all'` – Yogh Jun 06 '12 at 07:07
  • 1
    Unfortunately, just setting it to `Products.all` causes `undefined method 'current_page' for #` because it's expecting a Kaminari object. Looks like modifying the `params[:per_page]` is the only solution. – Amanda Jun 06 '12 at 07:15
  • @Amanda Sorry, by "basically works" I meant the idea of using querystring parameters, not the specific code in the answer. – Yogh Jun 06 '12 at 07:16
1

Try this to handle undefined method 'current_page'

=paginate @object if @object  && @object.try(:total_pages)
sreeraj nyros
  • 929
  • 1
  • 6
  • 17
0

This is how I've been doing it, since I need the helper methods that kaminari adds to the list of records:

params[:per] = Product.count if params[:per] == 'all'
Product.page(params[:page]).per(params[:per])

This will avoid the undefined method 'current_page' for #<Array:0x007fc6157c16a8> error. (Essentially @Yogh's suggestion in the comments of the other answer.)

smudge
  • 801
  • 1
  • 6
  • 21