Question

I recently changed the pagination with will_paginate in my Rails (2.3.4) app to use Ajax for the pagination and records per page. The pagination was done using the method described here: http://github.com/mislav/will_paginate/wiki/Ajax-pagination

I'm using this code in my view:

Records per page: <%= select_tag :per_page, options_for_select([4,8,24,100,500], @per_page.to_i), :onchange => remote_function(:url => users_path, :method => :get, :with => "'per_page=' + this.getValue()") %>

This works fine if I'm not viewing search results. But if I do a search and them attempt to change the records-per-page, my search results are lost and all records are returned. I'm pretty sure this is due to the url I'm calling in my remote_function, but I don't know how to fix it.

This is the code in my controller:

def index
@search = User.search(params[:search])
@search.order||="ascend_by_last_name"
if @search.count > 0
  @users = @search.paginate(:page => params[:page], :per_page => users_per_page )
  respond_to do |format|
    format.html # index.html.erb
    format.xml { render :xml => @users }
    format.csv { send_data @users.to_csv }
    format.js {
          render :update do |page|
            # 'page.replace' will replace full "results" block...works for this example
            # 'page.replace_html' will replace "results" inner html...useful elsewhere
            page.replace 'results', :partial => 'userview'
          end
      }      
    end
else
  flash[:notice] = "Sorry, your search didn't return any results."
  redirect_to users_path
end

end

Any help would be appreciated! Thanks.

Was it helpful?

Solution

You can append the per_page param to the end of the current query string.

:with => "'#{request.query_string}&per_page=' + this.getValue()"

This assumes there is a current query string, which could cause issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top