I have 2 models images
and categories
.
The relations are:
- a image belongs to one category.
- a category has many images.
On the index.html.erb
view from categories
object, I want paginate 20 images per page for all categories.
I mean, I want paginate all images from all categories with 20 images per page.
in my index action
on categories_controller.rb
method I have:
def index
@categories = Category.all
@categories.each do |category|
@images = Kaminari.paginate_array(category.images).page(1).per(1)
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @categories }
end
end
on index.html.erb I have:
<% @images.each do |image|%>
code for each image here
<% end %>
<% paginate @images %>
But this for me it does not works. I can not see anything image showed.
how I can implement this functionality? and fix this problem? Thank you.
@categories.each do |category|
@images = Kaminari.paginate_array(category.images).page(1).per(1)
end
In this loop, you're overriding @images each time, so obviously, you wont get what you expect ;)
I think that what you wanna do is:
@images = Image.where("category_id IS NOT NULL").page(params[:page]).per(20)
The .where("category_id IS NOT NULL")
is not necessary if images have to belong to a category.