The will_paginate (https://github.com/mislav/will_paginate) gem is a popular gem, that developers usually use for the pagination. It populates the links for the pagination and upon clicking the link, it will load the content. For adding UJS to these paginate links, we need to override the helper methods of the will_paginate gem. In the following, I am going to describe the steps to use UJS in will_paginate.
Step1:
We need to add a helper file (will_paginate_helper.rb) to our application. Here is the code to put in it.
module WillPaginateHelper class WillPaginateAjaxLinkRenderer < WillPaginate::ActionView::LinkRenderer def prepare(collection, options, template) options[:params] ||= {} options[:params]["_"] = nil @@remote = options[:remote] super(collection, options, template) end protected def link(text, target, attributes = {}) attributes['data-remote'] = @@remote if @@remote super end end def custom_will_paginate(collection, options = {}) will_paginate(collection, options.merge({:renderer => WillPaginateHelper::WillPaginateAjaxLinkRenderer, :remote => options[:remote]})) end end
We have added a custom method custom_will_paginate in it, which calls our custom helper methods in stead of will_paginate gem’s method. Here a new parameter has been introduced which is options[:remote] which returns the boolean value. As per the boolean value the link method adds the data-remote option to the will_paginate link.
Step2:
In the view file we are going to use our custom will_paginate helper meethod.
'color:blue',:params => {:controller => 'users', :action => 'active'}, :remote => true %>
We have added the extra parameter :remote => true to it.
Step3:
We need to include the custom helper method to our application.
include WillPaginateHelper
Now, it will populate the pagination link with data-remote = true attribute and the rest UJS will be managed by RAILS itsel