Reputation: 1749
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. I am battling a strange problem with will_paginate giving me the following error:
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.
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:
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
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.
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).
Would be very grateful for any pointers to move me in the right direction
Upvotes: 0
Views: 635
Reputation: 1047
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
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.
Upvotes: 1