mislav/will_paginate

undefined method total_pages error total_pages for #<Comment::ActiveRecord_AssociationRelation:0x47f7540>

Opened this issue · 2 comments

Hi, I’m having an issue with getting the ‘will_paginate’ gem to work. Here is the site I’m working on - https://kevin-meldau-my-test-store.herokuapp.com/products. This is the error I keep getting - https://snag.gy/BqEGAi.jpg

This is how I’ve loaded the gem:

  1. Added 'will_paginate', '~> 3.1.0' to my gem file then run bundle
  2. Added this to my products controller:

def show
@comments = @product.comments.order("created_at DESC")
@comments = @product.comments.paginate(page: params[:page], per_page: 5)
end

  1. Then added this to my show.html.erb file:

<%= will_paginate @comments %>

Any help would be greatly appreciated! All the best, Kevin

Maybe try

def show
  @comments = @product.comments
    .order(created_at: :desc)
    .paginate(page: params[:page], per_page: 5, total_pages: @product.comments.count)
end

Might be something to do with the way your relation is configured on the product.

Duplicate of #411 (comment)