Tips: Paginazione multipla con will_paginate
14:07TipsClaudio
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: rails, ruby, ruby on rails, Tips, will_paginate

















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
Hi Mislav,
thanks for your clarification!
Nice to see you on our blog.
Ruby on rail! I think very few use this language. But this pagination issue is good and informative.
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… ^_^