0

I'm trying to do pagination for products details(images), but I'm getting this error.

NoMethodError in Home#index Showing /home/aws002/webpage/app/views/home/index.html.erb where line #6 raised: undefined method `current_page' for #

Here is my products controler code:

def index
    @rroducts = Product.order(:name)
    Kaminari.paginate_array(@products).page(params[:page]).per(3)
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @products }
    end
end

and I'm trying to display in home page. view file code is:

<% @products=Product.all %>
  <%= paginate @products %>
  <% @products.each do |p|%>
    <%= image_tag("Images/#{p.id}.jpeg",:alt => p.name) %>
    <p> Price: </p>
    <%= p.price %>
    <p> Discount: </p>
    <%= p.discount %>
  <br/>
  <% end %>

Please help me to fix this. Thanks in advance.

user2218532
  • 331
  • 1
  • 5
  • 10

3 Answers3

3

Try this

def index
 @products = Product.order(:name).page(params[:page]).per(3)
 respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @products }
 end
end
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
KKB
  • 558
  • 4
  • 18
1

I got the answer.

Since I'm retrieving images which are saved in public directory and not using any controller or model to retrieve the images I've to include that kaminari part in view file only.

Kaminari.paginate_array(@products).page(params[:page]).per(3)

this should be added in view file.

user2218532
  • 331
  • 1
  • 5
  • 10
0

i think the error came from your index method as you declared instance variable called @rroducts since you actually using another for Kaminari called @products

Shimaa Marzouk
  • 429
  • 4
  • 10