Ruby on rails 将使用Rails 3对错误进行分页
我有一个由开发人员为我构建的小Rails应用程序,代码中有很多异常,我正在尝试纠正。我正在与will_paginate的一个奇怪问题作斗争,will_paginate给了我以下错误:Ruby on rails 将使用Rails 3对错误进行分页,ruby-on-rails,will-paginate,Ruby On Rails,Will Paginate,我有一个由开发人员为我构建的小Rails应用程序,代码中有很多异常,我正在尝试纠正。我正在与will_paginate的一个奇怪问题作斗争,will_paginate给了我以下错误: The @home variable appears to be empty. Did you forget to pass the collection object for will_paginate?` 我在谷歌上搜索了错误消息,但没有找到任何有用的方法来解决这个问题,所以我转向你,stackoverflo
The @home variable appears to be empty. Did you forget to pass the collection object for will_paginate?`
我在谷歌上搜索了错误消息,但没有找到任何有用的方法来解决这个问题,所以我转向你,stackoverflow
因此,我查找位置的搜索表单如下所示:
<%= form_for :search_place, :url => search_results_home_index_path, :html => {:class=>""} do |f| %>
<%= f.hidden_field :search_type, :value=>2 %>
<%= f.text_field :city,{:placeholder => "Town", :class=>"input-small"} %>
<%= f.text_field :county,{:placeholder => "County", :class=>"input-medium"} %>
<%= f.select(:country, db_countries.map(&:country), {:include_blank => 'Select a Country'}, :class=>"input-medium") %>
<%end%>
并从Location model Location.rb中提取集合
def self.search_location(search_params,page,per_page=50)
condition_str = ""
condition_str += "(UPPER(locations.town) like '%#{search_params[:city].upcase}%' OR UPPER(locations.town) like '%#{search_params[:city].upcase}%') AND " unless search_params[:city].blank?
condition_str += "(UPPER(locations.county) like '%#{search_params[:county].upcase}%' OR UPPER(locations.county) like '%#{search_params[:county].upcase}%') AND " unless search_params[:county].blank?
condition_str += "(UPPER(locations.country) like '%#{search_params[:country].upcase}%' OR UPPER(locations.country) like '%#{search_params[:country].upcase}%') " unless search_params[:country].blank?
unless condition_str.blank?
self.where(condition_str).paginate(:per_page=>per_page,:page=>page)
else
self.where("1=0").paginate(:per_page=>1,:page=>page)
end
end
搜索结果显示在搜索结果表中,如下所示:
<%= render "documents/document_common_list"%>
<%= will_paginate @documents, :renderer => BootstrapLinkRenderer %>
bootstraplingrenderer%>
因此,这将拉出正确的搜索结果,并显示正确的第一页和分页块,但如果我尝试访问其他页面,则会因上述错误(关于@home)而崩溃
will_paginate以这种方式在站点的其他地方工作得非常好,除了集合是在控制器中而不是模型中构建的,因此这可能是问题所在(我不完全确定哪种方法是正确的,但我正在使用其他人的代码,所以有点困难)
如果您能给我指点方向,我将不胜感激我对此并不完全确定,但我认为您遇到的问题如下所述:
基本上,您需要将表单上的http方法更改为GET(通过在表单标记中插入“:method=>”GET'),以便在以后查看搜索结果页面时不会丢失您的参数。很好,谢谢。时间不早了,我喝了一杯啤酒,但我会在早上测试一下,然后回来报告
<%= render "documents/document_common_list"%>
<%= will_paginate @documents, :renderer => BootstrapLinkRenderer %>