Pagination Gem with Bootstrap
Will Paginate in Rails 5
Steps
You can download the source code for this article from wpag.
Step 1
Add the gem to the Gemfile.
gem 'will_paginate', '>= 3.1'
Run:
bundle install
Step 2
def index
@tasks = Task.search(params[:term], params[:page])
end
Step 3
Add the will_paginate view helper to the tasks index page.
<%= will_paginate @tasks %>
Step 4This gives 10 tasks per page. So no pagination links are displayed on the browser. Because we only have 10 tasks.
Let's use the paginate method provided by
will_paginate
gem in the task model.def self.search(term, page)
if term
where('name LIKE ?', "%#{term}%").paginate(page: page, per_page: 5).order('id DESC')
else
paginate(page: page, per_page: 5).order('id DESC')
end
end
<div class="apple_pagination">
<div class="page_info">
<%= page_entries_info @tasks %>
</div>
<%= will_paginate @tasks, :container => false %>
</div>
You can change the look and feel of the pagination by changing the class to t
Comments
Post a Comment