Quantcast
Channel: database – Tech ABC to XYZ
Viewing all articles
Browse latest Browse all 31

Rails loads my @items n*n times [ANSWERED]

$
0
0

Rails loads my @items n*n times

Asked By: zargmelon
Originally Asked On: 2014-01-05 22:35:40
Asked Via: stackoverflow

I am creating a rails web app which basically just aggregates a list of users posts and allows them to vote and comment.

I was implementing endless scrolling by following the Railscast 114 – Endless Scrolling
post, although I have run into an issue that I am unsure if it is related to the endless scrolling, or just happened at the same time.

I am using the will_paginate gem. When the page is loaded it loads “n” number of posts the will_paginate call, but then those posts are displayed n times over.

For example:

@item.order(params[:sort]).page(params[:page]).per_page(3)

Would result in the following being displayed

item1
item2
item3
item1
item2
item3
item1
item2
item3

I have the following in my index action

@items = Item.order(sort_column + ' ' + sort_direction).page(params[:page]).per_page(5)

this is my _item partial

<% @items.each do |t| %>
<div id="Post1" class="shadow">
  <table>
    <tr>
      <td> 
        t.stuff....
      </td>
    </tr>
  </table>
</div>

And this is the render call in my index

<div id='items'>
  <%= render @items %>
</div>

It feels like its something simple, but I just cannot find it. Any suggestions on where to look would be much appreciated

He received 2 answers
eventually accepting:

zrl3dx’s answer to

Rails loads my @items n*n times

From my comment: change

<div id='items'>
  <%= render @items %>
</div>

to

<div id='items'>
  <%= render 'item' %>
</div>

Former version renders item partial for each object in @items collection (where you’re iterating again over each element), that’s why you get n*n items.

If the selected answer did not help you out, the other answers might!

All Answers For: Rails loads my @items n*n times

sameera207’s answer to

Rails loads my @items n*n times

I think it should be

Item.paginate... not Item.order… , see the will paginate guide here

zrl3dx’s answer to

Rails loads my @items n*n times

From my comment: change

<div id='items'>
  <%= render @items %>
</div>

to

<div id='items'>
  <%= render 'item' %>
</div>

Former version renders item partial for each object in @items collection (where you’re iterating again over each element), that’s why you get n*n items.

Of course, you should really check out the original question.

The post Rails loads my @items n*n times [ANSWERED] appeared first on Tech ABC to XYZ.


Viewing all articles
Browse latest Browse all 31

Trending Articles