【问题标题】:will_paginate error with Rails 3Rails 3 的 will_paginate 错误
【发布时间】:2012-12-05 21:00:41
【问题描述】:

我有一个由开发人员为我构建的小型 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 模型 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 以这种方式在网站的其他地方完美工作,除了集合是在控制器而不是模型中构建的,所以这可能是问题所在(我不完全确定哪种方法是正确的,但我'正在使用别人的代码,所以有点挣扎)。

非常感谢任何可以让我朝着正确方向前进的指针

【问题讨论】:

    标签: ruby-on-rails will-paginate


    【解决方案1】:

    我对此并不完全确定,但我认为您遇到的问题已在此处描述:

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

    基本上,您需要将表单上的 http 方法更改为 GET(通过在表单标记中插入 ":method => 'get'"),这样当您查看后面的页面时它不会丢失您的参数搜索结果。

    【讨论】:

    • 很好 - 谢谢。这里时间不早了,我已经喝了啤酒,但我会在早上测试这个并回来报告
    猜你喜欢
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2011-07-25
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    相关资源
    最近更新 更多