Using the Kaminari gem to paginate
#add gem to gemfile
gem 'kaminari'
bundle
page
scope that can be used on any ActiveReccord model (use the
params params[:page]
for this scope)#usage
class ProductsController < ApplicationController
def index
@products = Product.order("name").page(params[:page])
end
end
# before any relevant code
<%= paginate @products %>
# other relevant code ...
page
,
called per
. You can pass in the number of items per page as it's arguments.page
first on the ActiveRecord model.#usage
class ProductsController < ApplicationController
def index
@products = Product.order("name").page(params[:page]).per(5)
end
end
en.yml
file in config/locales/en.yml
# should contain the following
en:
hello: "Hello world"
# you can add the following:
views:
pagination:
previous: "< Previous"
next: "Next >"
truncate: "..." # default style is 3 dots
# if the array you are using is not an ActiveRecord array, but a generic ruby array
# you can still paginate the array by ading on the following:
@array = Kaminari.paginate_array(generic_array).page(params[:page]).per(4)
responds_to
a format block with options for html and jstable_sort
), make sure that it respond_to :js
!# in your .js.erb file:
$("#team-quote-table-body").html("<%= j render partial: 'team_quotes', locals: {team_quotes: @team_quotes} %>");
$('#paginator').html("<%= escape_javascript(paginate(@team_quotes, :remote => true).to_s) %>");
<%= paginate @model %>
is WRAPPED in a div with an id of paginator!<div id="paginator">
<% if @team_quotes && @team_quotes.length > 0 %>
<%= paginate @team_quotes, :remote => true %>
<% end %>
</div>
undefined method current_page for #
: this means the page
method is not being called on the collection being returned, recheck to see if your
later sql calls alter the results:@team_quotes = Fsi::Quote.where(quote_manager_id: @quote_manager.id).page(params[:page]).per(2) #calling it here is safe
# Click on the icon, check for header and sort
if !params["header"].blank? && !params["sort"].blank?
# check for the search param
if !params["search_term"].blank?
search = !params["search_term"].blank? ? params["search_term"] : ""
@team_quotes = @team_quotes.quick_search(search)
end
db_fields = {
"Created" => "created_at",
"Sales Rep" => "sales_rep"
}
header = db_fields[params["header"]]
sort = params["sort"].upcase if params["sort"]
@team_quotes = @team_quotes.order_by_created(sort) if header == "created_at"
@team_quotes = @team_quotes.order_by_sales_rep(sort) if header == "sales_rep"
else
# ONLY SEARCH
if !params["search_term"].blank?
search = !params["search_term"].blank? ? params["search_term"] : ""
@team_quotes = @team_quotes.quick_search(search)
end
end