简体   繁体   English

Rails will_paginate 错误:未定义的方法 `total_pages'

[英]Rails will_paginate error: undefined method `total_pages'

users_controller.rb: users_controller.rb:

@search_results = Notice.search('hello')
if(params[:query])
  @search_results = Notice.search(params[:query])
end

Notice.rb:注意.rb:

def self.search(search)
  if search
    Notice.where("content LIKE ?", "%#{search}%")
  else
  end
end

In the view:在视图中:

<%= render 'shared/search_results' %>

_search_results.html.erb partial: _search_results.html.erb 部分:

<% if @search_results.any? %>
  <ol class="notices">
    <%= render @search_results %>
  </ol>
  <%= will_paginate @search_results %>
<% end %>

I get the error: undefined method total_pages for #<Notice::ActiveRecord_Relation:0x0000010f3e8888> .我收到错误: undefined method total_pages for #<Notice::ActiveRecord_Relation:0x0000010f3e8888>

(It all works fine without pagination.) (在没有分页的情况下一切正常。)

How do I fix this error?我该如何解决这个错误?

From the will paginate documentation:从将分页文档:

## perform a paginated query:
@posts = Post.paginate(:page => params[:page])

# or, use an explicit "per page" limit:
Post.paginate(:page => params[:page], :per_page => 30)

## render page links in the view:
<%= will_paginate @posts %>

So, for your code you need to do:因此,对于您的代码,您需要执行以下操作:

search_results = Notice.search('hello').paginate(page: params[:page])
if(params[:query])
  @search_results = Notice.search(params[:query]).paginate(page: params[:page])
end

or the new Syntax in ActiveRecord 3或 ActiveRecord 3 中的新语法

search_results = Notice.search('hello').page(params[:page])
if(params[:query])
  @search_results = Notice.search(params[:query]).page(params[:page])
end

I had this same challenge when working on a Rails 6 application.在处理 Rails 6 应用程序时,我遇到了同样的挑战。

This was my code:这是我的代码:

def index
  if params[:query].present?
    @products = Product.search(params[:query])
  else
    @products = Product.paginate(page: params[:page], per_page: 30)
  end
  @brands = Brand.all
  @categories = Category.all
end

So whenever I try searching for products, it throws the error:所以每当我尝试搜索产品时,它都会抛出错误:

undefined method `total_pages' for #Product::ActiveRecord_Relation:0x00007f1f802423d0 #Product::ActiveRecord_Relation:0x00007f1f802423d0 的未定义方法 `total_pages'

Here's how I fixed it :这是我修复它的方法

I simply added the paginate method to the if statement as well:我也只是在if语句中添加了paginate方法:

paginate(page: params[:page], per_page: 30)

So my code looked like this after that:所以之后我的代码看起来像这样:

def index
  if params[:query].present?
    @products = Product.search(params[:query]).paginate(page: params[:page], per_page: 30)
  else
    @products = Product.paginate(page: params[:page], per_page: 30)
  end
  @brands = Brand.all
  @categories = Category.all
end

That's all.就这样。

I hope this helps我希望这有帮助

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

相关问题 Rails:Ransack和will_paginate(未定义的方法“ total_pages”错误) - Rails: Ransack and will_paginate (undefined method 'total_pages' error) will_paginate 未定义的方法 `total_pages&#39; - will_paginate undefined method `total_pages' will_paginate不起作用,返回未定义方法“ total_pages”的错误 - will_paginate not working, returning an error of undefined method `total_pages' will_paginate -error-undefined method`total_pages&#39; - will_paginate -error-undefined method `total_pages' Will_paginate 错误未定义的方法 `total_pages&#39; - Will_paginate error undefined method `total_pages' Will_Paginate gem错误未定义方法“ total_pages” - Will_Paginate gem error undefined method “total_pages” Rails-will_paginate和ActionCable服务器广播冲突并导致错误(未定义的方法“ total_pages”) - Rails - will_paginate and ActionCable server broadcast are conflicting and causing an error (undefined method `total_pages') Rails:will_paginate +简单搜索引擎错误:未定义的方法“ total_pages” - Rails: will_paginate + simple search engine error: undefined method `total_pages' 使用will_paginate进入未定义的方法“ total_pages” - Running into undefined method `total_pages' with will_paginate 数组 will_paginate rspec 的未定义方法 total_pages&#39; - undefined method total_pages' for array will_paginate rspec
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM