Last Updated: March 09, 2016
· tmartin314

Rails 4 per page form for displaying record limit

We needed a way to send a per page parameter while preserving the other params in the URL:

Create a form with hidden fields

= form_tag per_page_path, method: :get, data: { behavior: 'per-page-form' }
  = hidden_field_tag :page, params[:page] if params[:page]
  = hidden_field_tag :order, params[:order] if params[:order]
  = hidden_field_tag :direction, params[:direction] if params[:direction]
  = label_tag "Per Page"
  = select_tag :per_page, options_for_select([10,20,50], params[:per_page].to_i), data: { behavior: "per-page" }

Listen for the select field to change, and submit the form:

$ ->
  $('[data-behavior~=per-page]').change ->
    form = $('form[data-behavior~=per-page-form]').submit()