rss
twitter
    Find out what I'm doing, Follow Me :)

Ajax Pagination in rails

Ajax Pagination in Rails is very simple and DRY. You Just have to put ajax helper method in applicartion .rb. And its done. Here is the procedure.:

1) Put the following line of code in apllication_helper.rb file.

def ajax_pagination_links(paginator, options={})

options.merge!(ActionView::Helpers::PaginationHelper::DEFAULT_OPTIONS) {key, old, new old}
window_pages = paginator.current.window(options[:window_size]).pages
return if window_pages.length <= 1 unless options[:link_to_current_page] first, last = paginator.first, paginator.last returning html = '' do if options[:always_show_anchors] and not window_pages[0].first? html << update =""> options[:update], :url => { options[:name] => first }.update(options[:params] ))
html << ' ... ' if window_pages[0].number - first.number > 1
html << ' ' end window_pages.each do page if paginator.current == page && !options[:link_to_current_page] html << update =""> options[:update], :url => { options[:name] => page }.update(options[:params] ))
end
html << ' ' end if options[:always_show_anchors] && !window_pages.last.last? html << ' ... ' if last.number - window_pages[-1].number > 1
html << update =""> options[:update], :url => { options[:name] => last }.update( options[:params]))
end
end
end



2) Now in the controller in you method put the following code.

def user
@content_pages, @contentdata = paginate :users, :order =>"create_date DESC", :per_page => 6
if params[:page]
render :update do page
page[:center].replace_html :partial=>'user'
end
else
render :action=>'user'
end
end



3) And in the last just put these lines in the view part (rhtml) and its done.



<%=ajax_pagination_links @content_pages, {:params => {:search_query => @params[:search_query]} }%>

0 comments: