Saturday, May 30, 2009

Pagination in Ruby on Rails

* Add following function in application.rb helper file


def pagination_links_remote(paginator)
page_options = {:window_size => 1}
pagination_links_each(paginator, page_options) do |n|
options = {
:url => {:action => 'list', :params => params.merge({:page => n})},
:update => 'table',
:before => "Element.show('spinner')",
:success => "Element.hide('spinner')"
}
html_options = {:href => url_for(:action => 'list', :params => params.merge({:page => n}))}
link_to_remote(n.to_s, options, html_options)
end
end


* Add code to fetch paginated results in controller file
e.g. @declined_patient_application_pages, @declined_patient_application = paginate :patient_applications, :per_page => 1, :conditions => { :application_status => 'Declined', :surgeon_id => session[:surgeon_login]}

* Call pagination in rhtml file
<%= pagination_links_remote(@declined_patient_application_pages)%>