users_controller.rb:
@search_results = Notice.search('hello')
if(params[:query])
@search_results = Notice.search(params[:query])
end
Notice.rb:
def self.search(search)
if search
Notice.where("content LIKE ?", "%#{search}%")
else
end
end
在视图中:
<%= render 'shared/search_results' %>
_search_results.html.erb局部文件:
<% if @search_results.any? %>
<ol class="notices">
<%= render @search_results %>
</ol>
<%= will_paginate @search_results %>
<% end %>
我遇到了错误:
#<Notice::ActiveRecord_Relation:0x0000010f3e8888>
没有定义total_pages方法。
(没有分页时一切正常。)
我该如何修复这个错误?
.paginate
方法:@search_results = @search_results.paginate(page: params[:page], per_page: 20)
- MrYoshiji