Tips: Paginazione multipla con will_paginate

Will Paginate è la gemma più utilizzata per la paginazione in Ruby on Rails.

Succede a volte di dover paginare più liste diverse su una stessa pagina.

Questo tip mostra come è possibile visualizzare una lista paginata di prodotti ed una lista paginata di servizi su una stessa pagina utilizzando will_paginate.
Il punto chiave sta nell’utilizzare un parametro param_name personalizzato sia nella view che nel controller.

Il codice della view sarà qualcosa di simile a questo:

1
2
3
4
5
6
7
<br />
# ...<br />
# la lista di prodotti va qui<br />
  <%= will_paginate @products, :param_name => 'products_page', :prev_label => t('helpers.labels.previous'), :next_label => t('helpers.labels.next') %></p>
<p># la lista di servizi va qui<br />
  <%= will_paginate @services, :param_name => 'services_page', :prev_label => t('helpers.labels.previous'), :next_label => t('helpers.labels.next') %><br />
# ...<br />

E questo invece sarà il codice del controller:

1
2
3
4
5
6
7
<br />
# ...<br />
  def index<br />
    @products = current_company.products.paginate(:page => params[:products_page], :per_page => 10)<br />
    @services = current_company.products.paginate(:page => params[:services_page], :per_page => 10)<br />
  end<br />
# ...<br />

Tags: , , , ,


About Claudio

Claudio Marai is a co-founder of DevInterface.

After graduating in Computer Science has contributed to develop complex web applications based on Java/J2EE and desktop applications with the. NET framework for the Ministry of Justice and ultimately for the banking ambit.

The passion for web in recent years has led him to be interested in more modern frameworks such as Ruby on Rails and Django, and to a development approach based on agile methodologies such as eXtreme Programming and SCRUM.

About DevInterface

We are an information and communication technology agency. Our mission is to provide web application development, design services and communication strategies. We specialize in building web applications with modern and efficient frameworks.

Related Post

4 Responses to “Tips: Paginazione multipla con will_paginate”

  1. Mislav scrive:

    The :prev_label parameter has been renamed to :previous_label. The old name is deprecated.

    However, you shouldn’t customize the labels this way. Instead, you shoud override the default translations in your locale files: https://github.com/mislav/will_paginate/wiki/I18n

  2. Claudio scrive:

    Hi Mislav,
    thanks for your clarification!

    Nice to see you on our blog. :-)

  3. Designarcade scrive:

    Ruby on rail! I think very few use this language. But this pagination issue is good and informative.

  4. Aj Banda scrive:

    wow thanks for this.. I was using will_paginate for sometime but this would be my first time on using multiple pagination in one page… ^_^

Leave a Reply

Insert code beetween <code lang="ruby"> and </code>

Copyright 2012 DevInterface s.n.c.

DevInterface Blog is proudly powered by WordPress