Question

I am new to RoR so I'm missing something very basic here, but I can't figure it out. I'm using Kaminari gem for pagination in RoR application by calling in controller:

users_controller.rb:

def index
@users =  User.order('created_at DESC').page(params[:page])
end

In my view I have:

users/index.html.erb:

<%= paginate @users %>
<%= render @users %>

Finally, my user partial is:

users/_user.html.erb

<% @users.each do |u| %>
(data output like <%= u.login %> and so on)
<%= end %>

So it is as simple as it can be and similar to Kaminari usage example. However, I receive proper table with specified number of rows but that table is rendered n times where n is number of paginates_per option. Pagination ifself works fine, but I can't debug that problem, what I'm doing wrong?

I have used will_paginate gem before Kaminari and it worked fine out of the box, so my app is not completely broken somewhere deeper (if it could be in such simple case).

According to the logs everything is rendered once so the @users object contains multiplied data.

Was it helpful?

Solution 2

Cause of this problem is very simple, one answer is to do change in

users/index.html.erb:

from:

<%= paginate @users %>
<%= render @users %>

to:

<%= paginate @users %>
<%= render 'user' %> 

but if you (or rather I) want to render collection I should have in my partial

users/_user.html.erb:

  <tr>
    <td><%= user.id %></td>
    <td><%= user.login %></td>
    <td><%= user.email %></td>
    #(and so on...)
    <% end %>
  </tr>

and only that (part of table that will be called for each record from database)

so now index.html.erb looks like that:

users/index.html.erb

<table>
  <tr>
    <th>Id</th>
    <th>Name</th>
    #(and whatever you want)
  </tr>

  <%= render @users %>
</table>

Now the table will render properly.

OTHER TIPS

<%= @users.each do |u| %>
(data output like <%= u.login %> and so on)
<%= end %>

<%= outputs the code results, while <% just execute it. Swap you code with:

<% @users.each do |u| %>
(data output like <%= u.login %> and so on)
<% end %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top