refinery/refinerycms-page-images

Adding Pagination to view

denisinla opened this issue · 12 comments

I currently have a page with 10+ images being rendered. Is there a way to add pagination to this engine and set a total of 5 images to display before you're required to head to the next page.

@dkabistan frontend or backend?

@parndt This would be on the front-end, currently I'm displaying 20 images on the front-end without pagination(long scroll). Any ideas as to how to paginate, after roughly 5 images?

This extension purposefully stays out of the frontend code so I'm afraid this is up to you. I would suggest using a pagination plugin like will_paginate as refinerycms-core already depends on it.

@parndt Does that mean that I can't override the pages-images controller to add paginate with limit amount. If so please advise naming, since I've exhausted finding it.

@dkabistan there is no page-images controller.. All this extension really does is add an images relationship to refinerycms-pages and also a backend UI for managing that relationship inside the page's form.

So, on the frontend, you can refer to it using @page.images (assuming you have a @page variable.. which you usually will)

How are you currently using this extension on the frontend?

@parndt following the same implementation as refinerycms-page-images except using a custom view template. I was not aware of it being this simple. I'll try out using will_paginate.

Thanks !

@dkabistan no problem.. if you have any trouble, feel free to post again to here. I just closed the issue as it's not an "issue" with the extension but am happy to help if I can.

@parndt I followed the wiki to initiate pagination for @page.images on the front-end, the pagination works but the per_page limit set is not picked up. Any ideas?

<%= render '/refinery/content_page' %>
<section id="box">
  <article class="content">
    <% @page.images.each_with_index do |image, index| %>
      <figure class="press-item">
      <%= link_to image_tag(image.thumbnail(geometry: '242x316').url),
                            image.thumbnail(geometry: '900x600').url,
                            class: 'fancybox',
                            rel: 'gallery'
      %>
        <figcaption><%= raw @page.caption_for_image_index(index) %></figcaption>
      </figure>
    <% end %>
  </article>
  <%= will_paginate @page.images.paginate :page => params[:page], :per_page => 5 %>
</section>

@dkabistan given you're calling a will_paginate helper it should be working like this, according to the docs.

You could try adding brackets?

<%= will_paginate @page.images.paginate(:page => params[:page], :per_page => 5)

Turns out I needed to define paginate usage on an array(d'oh): Stackoverflow Thread

will_paginate 3.0 is designed to take advantage of the new ActiveRecord::Relation in Rails 3, so it defines paginate only on relations by default. It can still work with an array, but you have to tell rails to require that part.

In a file in your config/initializers/will_paginate_extension.rb, add this

require 'will_paginate/array'

Then you can use on arrays

my_array.paginate(:page => params[:page], :per_page => y)

@dkabistan woohoo 👍

@parndt I'm having a bit of an issue figuring this one out. I have successfully paginated my images on the front-end but for some reason my captions are not in sync when you click on the next page.

I've tried adding paginate to @page.caption_for_image_index(index) but unsuccessfully. Any ideas how to go about?