Search code examples
ruby-on-rails-3kaminari

Pagination in ruby on rails


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?


Solution

  • Ref this

    I think you forgot to add following line in your gemfile

    gem 'kaminari'
    

    and then bundle install