我是RoR的新手,我正在上一门课程。我已经实现了ActiveRecord查询以及will_paginate gem,两者都可以单独工作。ActiveRecord查询在添加分页时停止工作| Rails 5 |
然而,当我增加了分页功能,查询搜索断裂,给我以下错误:
NoMethodError在产品#指数 未定义的方法`TOTAL_PAGES'为#
Extracted source (around line #7):
<div class="container-fluid">
<div class="pages">
<%= will_paginate @products %>
</div>
<div class="row">
Gemfile
gem 'will_paginate'
products_controller.rb
def index
if params[:q]
search_term = params[:q]
if (Rails.env == "production")
@products = Product.where("name ilike ?", "%#{search_term}%")
else
@products = Product.where("name LIKE ?", "%#{search_term}%")
end
else
@products = Product.all.paginate(:page => params[:page], :per_page => 3)
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
application.html.erb
<li class="nav-search">
<%= form_tag("/products", method: "get") do %>
<%= text_field_tag(:q) %>
<%= submit_tag("Go!", class: 'button-s') %>
<% end %>
</li>
谢谢你的片段。代码看起来更清洁这种方式比我原来的更正:) – catch22