0

I'm trying to get the next page link. How would I do this? I get the following error when calling link_to_next_page

undefined method `link_to_next_page'

query = Posts.page(1).per(5).includes(author: :profile)

link = link_to_next_page(query, 'Next-Page')
aryaxt
  • 76,198
  • 92
  • 293
  • 442
  • Are you sure you installed the `gem kaminari` correctly? Can you ensure that `@posts = Post.page(1).per(5)` `<%= paginate @posts %>` is working? – Vibol Jun 27 '14 at 00:58
  • Prehaps you using `link_to_next_page` helper somewhere outside of rails templates? – railscard Jun 27 '14 at 04:21
  • @railscard Yes, I'm using it in my controller. I need to get the next-page link and add it to the header of my response. Is that possible? – aryaxt Jun 27 '14 at 05:15
  • have you refer this video explain very nicely.. http://railscasts.com/episodes/254-pagination-with-kaminari?view=comments – Gagan Gami Jun 27 '14 at 05:36
  • If you want to put RFC 5988 Link headers, consider using [api-pagination](https://github.com/davidcelis/api-pagination#api-pagination) – Yuki Nishijima Jun 28 '14 at 04:51
  • I ended up doing my own pagination, this library didn't do what I was expecting it to do. – aryaxt Jun 29 '14 at 18:01

2 Answers2

1

Link helpers are not accessible into controllers. You can include entire helper module into your controller, but better use view_context to access particular helper method:

query = Posts.page(1).per(5).includes(author: :profile)    
link = view_context.link_to_next_page(query, 'Next-Page')

Good luck!

dubadub
  • 3,312
  • 3
  • 23
  • 28
0
def paginate(query)
   query.offset!((@page-1) * @per_page)
   query.limit!(@per_page+1)

   result = query.to_a

   if (result.size > @per_page)
     result.pop
     response.headers['Link'] = CREATE_NEXT_AGE_LINK_HERE
   end

   result
end
aryaxt
  • 76,198
  • 92
  • 293
  • 442