【问题标题】:Search box in ruby on rails not displaying results with paginationruby on rails 中的搜索框不显示分页结果
【发布时间】:2018-05-20 12:17:43
【问题描述】:

我正在使用 Ruby on Rails,试图让我的搜索栏显示结果,但每当我分页到产品索引页面时,我都会遇到以下错误:

我的搜索表单:

<div class="navbar-form navbar-right search-form" role="search">
    <div class="input-group add-on">
        <%= form_with(url: products_path, method: 'get', local: true) do |form| %>
        <%= form.text_field :q, id: 'q', class:"form-control", placeholder: "Search ...", value: params[:q] %>
        <div class="input-group-btn">
            <%= form.submit 'Go', class:"btn btn-default", id:"btn-search"%>
        </div><!-- input-group-btn -->
        <% end %>
    </div>
</div>

在我的观点中,我有:

<div class="text-center">
  <%= will_paginate @products, inner_window: 1, outter_window: 0 %>
</div>

我的产品控制器:

def index
    if params[:q]
      search_term = params[:q]
      @products = Product.search(search_term)
    else
      @products = Product.all.paginate(page: params[:page], per_page: 6)
    end
  end

我的产品型号:

def self.search(search_term)
    if Rails.env.production?
      Product.where("name ilike ?", "%#{search_term}%")
    else
      Product.where("name LIKE ?", "%#{search_term}%")
    end
  end

任何帮助将不胜感激!谢谢!

【问题讨论】:

标签: ruby-on-rails ruby will-paginate search-form


【解决方案1】:

在您的索引操作中应该是Product.search(search_term).paginate(page: params[:page], per_page: 6)

【讨论】:

    猜你喜欢
    • 2014-06-19
    • 2013-12-23
    • 2016-04-07
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 2015-08-13
    相关资源
    最近更新 更多