M.SH
M.SH

Reputation: 378

undefined method total_pages in will_paginate gem

I have got an error in will_paginate gem

In the controller

@like_posts = current_user.likes.order("created_at DESC").page(params[:page]).per_page(10).map(&:post)

When I use in the view

<%= will_paginate(@like_posts) %><br/>
<%= page_entries_info(@like_posts) %><br/>

I got this error

undefined method `total_pages' for #<Array:0xaae2dec>

24: <%= will_paginate(@like_posts) %><br/>
25: <%= page_entries_info(@like_posts) %><br/>

and if i change make this in the controller

   @like_posts = current_user.likes.order("created_at DESC").map(&:post).page(params[:page]).per_page(10)

i have this error

 undefined method `page' for#<Array:0xabcd34c>

any help?

Upvotes: 1

Views: 4936

Answers (2)

NZisKool
NZisKool

Reputation: 239

That may be solved by adding require 'will_paginate/array' at the top of your application_controller.rb:

require 'will_paginate/array'

class ApplicationController < ActionController::Base
end

Upvotes: 0

M.SH
M.SH

Reputation: 378

the main problem of me that i want the pagination to work on static array and it is very dangerous so will_paginate refuse it and like to work with the query from the database

here is the trouble shooting of will_paginate

https://github.com/mislav/will_paginate/wiki/Troubleshooting

i solve it by make 2 objects

   @likes = current_user.likes.order("created_at DESC").page(params[:page]).per_page(5)
   @like_posts = @likes.map(&:post)

in view

  <%= will_paginate @likes %><br/>
  <%= page_entries_info @likes %><br/>

use @likes for pagination and @like_posts to iterate on posts

Upvotes: 1

Related Questions