使用 Rails 和 Ajax 更改返回的搜索结果数量
我最近在 Rails (2.3.4) 应用程序中使用 will_paginate 更改了分页,以使用 Ajax 进行分页和每页记录。分页是使用此处描述的方法完成的: http://github.com/mislav/will_paginate /wiki/Ajax-pagination
在我看来,我正在使用此代码:
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()") %>
如果我不查看搜索结果,这效果很好。但是,如果我进行搜索并且他们尝试更改每页记录,我的搜索结果将丢失并返回所有记录。我很确定这是由于我在 Remote_function 中调用的 url 造成的,但我不知道如何修复它。
这是我的控制器中的代码:
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
任何帮助将不胜感激!谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将 per_page 参数附加到当前查询字符串的末尾。
这假设存在当前查询字符串,这可能会导致问题。
You can append the per_page param to the end of the current query string.
This assumes there is a current query string, which could cause issue.