与Rails 3的will_paginate错误

时间:2012-12-05 21:00:42

标签: ruby-on-rails will-paginate

我有一个由开发人员为我构建的小型Rails应用程序,并且代码中有许多我正在尝试纠正的异常。我正在与will_paginate争论一个奇怪的问题,给我以下错误:

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%>

其中引用了home_controller.rb:

def search_results
  :
  elsif !params[:search_place].blank?
    session[:search_params] = params[:search_place]
    @documents = Location.search_location(params[:search_place], params[:page])
  end

从位置模型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 %>

因此,这会提取正确的搜索结果并显示正确的第一页和分页块,但如果我尝试访问其他页面,则会出现上述错误(约@home)崩溃。

will_paginate以这种方式在网站的其他地方完美地工作,除了集合是在控制器而不是模型中构建的所以这可能是问题(我不完全确定哪个是正确的方法来做到这一点,但我我正在与其他人的代码合作,所以有点挣扎。

非常感谢有任何指示让我朝着正确的方向前进

1 个答案:

答案 0 :(得分:1)

我对此并不完全确定,但我认为你所遇到的问题在这里描述:

https://github.com/mislav/will_paginate/wiki/Simple-search

基本上,您需要将表单上的http方法更改为GET(在表单标记中插入“:method =&gt;'get'”),以便在查看以后的页面时不会丢失参数搜索结果。