I am trying to do a "Twitter like" pagination with a "More" button to load results with Kaminari.
I found this question here
But I can't figure out how to make it works and if it's a good approach.
Thanks in advance
Do you mean you need a "more" button? How about creating a helper like this?
# usage:
# link_to_next_page(@items)
# link_to_next_page(@items, :remote => true) # Ajax
def link_to_next_page(scope, name, options = {}, &block)
param_name = options.delete(:param_name) || Kaminari.config.param_name
link_to_unless scope.last_page?, name, {param_name => (scope.current_page + 1)}, options.merge(:rel => 'next') do
block.call if block
end
end
I'm ready to include this kind of helper methods to the gem if you find it useful, so please let me know what you think. Thanks!
Keep in mind that link_to_next_page(@items, :remote => true)
won't work correctly out of the box. Since it has no way to determine the current page after an Ajax request, the link needs to be replaced after new items are fetched. Using unobtrusive javascript, this would something look like this:
# app/views/items/index.js.erb
$(".items").append("<%= escape_javascript(render(@items)) %>");
$(".more_link").replaceWith("<%= escape_javascript(
link_to_next_page @items, 'View more',
:remote => true,
:id => :view_more) %>");
If this doesn't make sense, take a look at the Unobtrusive Javascript screencast at Railscasts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With