2012-12-05 58 views
0

我有一個由開發人員爲我構建的小型Rails應用程序,並且代碼有很多異常,我嘗試更正。我爭奪一個奇怪的問題,will_paginate給我下面的錯誤:Rails 3 will_paginate錯誤

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

我GOOGLE了錯誤信息,但沒有發現任何解決這個問題有用的,所以我轉交給你,計算器。

所以我的搜索形式找到一個位置,看起來像這樣:

<%= 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引用這樣:從選址模型location.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 

並拿起集合

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

我不能完全肯定這一點,但我認爲你有問題說明如下:

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

基本上,您需要將表單上的http方法更改爲GET(在表單標記中插入「:method =>'get'」),以便在查看搜索結果的後續頁面時不會丟失參數。

+0

好 - 趕快 - 謝謝。這裏遲到了,我已經喝了一杯啤酒,但是我會在早上測試這個,並且報告回來 –

+0

剛剛測試過並且很有效 - 非常感謝 –

+0

沒問題。很高興聽到它的工作。 – RoryB