0

I had got a task to do pagination.When i am doing pagination i had got these error

NoMethodError in ProductsController#index

undefined method `page' for []:ActiveRecord::Relation

Rails.root: /home/nithinv/store
Application Trace | Framework Trace | Full Trace

app/controllers/products_controller.rb:4:in `index'

This is my controller

 def index

       @products = Product.order(:name).page(params[:page]).per(2)

      respond_to do |format|
        format.html  #index.html.erb
        format.json { render :json=> @products }
      end
     end

This is my index.html.erb

 <% title "Products" %>

<%= paginate @products %>

<% for product in @products %>
  <div class="product">
    <h2><%= link_to product.name, product %></h2>
    <div class="details">
      <%= number_to_currency(product.price) %> |
      Released <%= product.released_at.strftime("%B %e, %Y") %>
    </div>
  </div>
<% end %>

<p><%= link_to "New Product", new_product_path %></p>

How can i solve this problem?

Nithin Viswanathan
  • 3,245
  • 7
  • 39
  • 84

2 Answers2

0

Ref this

I think you forgot to add following line in your gemfile

gem 'kaminari'

and then bundle install

Salil
  • 46,566
  • 21
  • 122
  • 156
0

I looks that the problem has to do with kaminari gem configuration (not your code). I'd recommend you to do this quick check:

  1. Run bundle install
  2. Restart the server (because changes may not have been loaded)
  3. Check your gemfile because, kaminari gem may is misplaced (under :test group)

A bit obvious, but in occasions I've also found myself wasting time after forgetting some basic checks.

gradenauer
  • 474
  • 3
  • 5