开发者

Changing number of returned search results with Rails and Ajax

开发者 https://www.devze.com 2023-03-05 06:17 出处:网络
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.

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消