gpt4 book ai didi

ruby-on-rails - Rails will_paginate 错误 : undefined method `total_pages'

转载 作者:行者123 更新时间:2023-12-04 19:51:48 30 4
gpt4 key购买 nike

users_controller.rb:

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

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

在 View 中:
<%= render 'shared/search_results' %>

_search_results.html.erb 部分:
<% if @search_results.any? %>
<ol class="notices">
<%= render @search_results %>
</ol>
<%= will_paginate @search_results %>
<% end %>

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

(在没有分页的情况下一切正常。)

我该如何解决这个错误?

最佳答案

从将分页文档:

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

因此,对于您的代码,您需要执行以下操作:
search_results = Notice.search('hello').paginate(page: params[:page])
if(params[:query])
@search_results = Notice.search(params[:query]).paginate(page: params[:page])
end

或 ActiveRecord 3 中的新语法
search_results = Notice.search('hello').page(params[:page])
if(params[:query])
@search_results = Notice.search(params[:query]).page(params[:page])
end

关于ruby-on-rails - Rails will_paginate 错误 : undefined method `total_pages' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29778011/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com