简体   繁体   English

Rails 3的will_paginate错误

[英]will_paginate error with Rails 3

I have a small Rails app built for me by a developer, and the code has lots of anomalies that I'm trying to correct. 我有一个由开发人员为我构建的小型Rails应用程序,并且代码中有很多我要纠正的异常。 I am battling a strange problem with will_paginate giving me the following error: 我正在与will_paginate对抗一个奇怪的问题,给我以下错误:

The @home variable appears to be empty. Did you forget to pass the collection object for will_paginate?`

I've googled the error message, but have not found anything useful in solving this problem, so I turn to you, stackoverflow. 我已经搜索了错误消息,但没有找到解决该问题的有用方法,所以请您转向stackoverflow。

So my search form to find a location looks like this: 因此,我的查找位置的搜索表单如下所示:

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

Which references home_controller.rb as such: 其中这样引用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

And picks up the collection from the Location model location.rb 并从Location模型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

The search results are displayed in a search results table as follows: 搜索结果显示在搜索结果表中,如下所示:

<%= render "documents/document_common_list"%> 
<%= will_paginate @documents, :renderer => BootstrapLinkRenderer %>

So this pulls out the correct search results and displays the correct first page and the paging block, but crashes with the above error (about @home) if I try to access other pages. 因此,这会拉出正确的搜索结果并显示正确的首页和分页块,但是如果我尝试访问其他页面,则会因上述错误(关于@home)而崩溃。

will_paginate works perfectly elsewhere on the site in this way, except the collection is built in the controller rather than the model so this may be the problem (I'm not entirely sure which is the correct way to do this, but I'm working with someone else's code so struggling a bit). will_paginate可以通过这种方式在网站上的其他任何地方正常工作,除了集合是在控制器而不是模型中构建的,因此这可能是问题所在(我不完全确定哪种方法是正确的,但是我正在工作与别人的代码有点挣扎)。

Would be very grateful for any pointers to move me in the right direction 非常感谢任何指导我朝着正确方向前进的人

I'm not entirely sure about this, but I think the problem you have is described here: 我对此并不完全确定,但是我认为您遇到的问题在这里描述:

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

Basically, you need to change the http method on your form to GET (by inserting ":method => 'get'" in you form tag) , so that it doesn't lose your parameters when you view later pages of search results. 基本上,您需要将表单上的http方法更改为GET(通过在form标记中插入“:method =>'get'”),以便在以后查看搜索结果页面时不会丢失参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM