【发布时间】:2017-01-24 12:26:59
【问题描述】:
我是 RoR 的新手,我正在在线学习课程。我已经实现了 ActiveRecord 查询以及 will_paginate gem,并且两者都可以单独工作。
但是,在我添加分页后,查询搜索中断并出现以下错误:
产品中的 NoMethodError#index #
的未定义方法“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>
【问题讨论】:
标签: ruby-on-rails activerecord pagination