0

I'm working with Ruby on Rails, trying to get my search bar to display the results, but whenever I had pagination to the products index page I'll have the following error:enter image description here

My search form:

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

In my Views I have:

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

My Products controller:

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

My Products model:

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

any help would be greatly appreciated! Thanks!

Andreia
  • 167
  • 1
  • 2
  • 9
  • Similar to [this](https://stackoverflow.com/questions/1408852/will-paginate-undefined-method-total-pages) – BigRon May 20 '18 at 13:03

1 Answers1

0

Should be Product.search(search_term).paginate(page: params[:page], per_page: 6) in your index action.

yzalavin
  • 1,788
  • 1
  • 13
  • 19