# Problem with partial rendering for groups of objects

**URL:** <https://www.ruby-forum.com/t/problem-with-partial-rendering-for-groups-of-objects/187299>\
**Category:** Rails\
**Created:** [April 21, 2010, 1:31pm UTC](https://www.ruby-forum.com/t/problem-with-partial-rendering-for-groups-of-objects/187299 "2010-04-21T13:31:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adler\_Santos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@Adler\_Santos](https://www.ruby-forum.com/u/Adler_Santos)\
**Post date:** [April 21, 2010, 1:31pm UTC](https://www.ruby-forum.com/t/problem-with-partial-rendering-for-groups-of-objects/187299/1 "2010-04-21T13:31:54Z")

</div>

I have a total of 30 Project thumbnails that I’m planning to render in  
groups of 8 in my index page. I’m going to put a “next” button that  
will replace the present group with the next 8 thumbnails, and so on  
until one gets to the final group of thumbnails. My codes are working  
but only for the first two groups: the first group is rendered when  
one loads the index page, the 2nd group when one presses the “next”  
button. Pressing the “next” button further doesn’t change anything,  
the 2nd group isn’t replaced by anything. Anyway, I used AJAX and  
partials to do this and here are my codes:

In the index view:  
\<%= render(:partial =\> “thumbnail”, :collection =\> @newthumbs) %\>

\<% form\_remote\_tag :url =\> {:action =\> “next”, :remaining =\>  
@everything} do %\> #next button  
\<%= submit\_tag “Next” %\>  
\<% end %\>

In the next.js.rjs:  
page.replace\_html(“thumbnail” , :partial =\> “thumbnail” , :collection  
=\> @newthumbs)

In the \_thumbnail.html.erb partial:  
\<%= image\_tag thumbnail.image %\> #just to output a project’s image

In the Controller:  
def index  
@everything = Project.find(:all).reverse #since I want to  
render the newest projects first  
@newthumbs = [] #collection that partial \_thumbnail.html.erb will  
replace with  
for project in @everything do  
if @newthumbs.size \< 8 #inputs 8 objects for the first group  
@newthumbs \<\< project  
end  
end  
@remaining = @everything - @newthumbs#sets the remaining  
thumbnails to be rendered  
end

def next  
@newthumbs = []  
@everything = []  
for remain\_id in params[:remaining] do  
project = Project.find(remain\_id)  
if @newthumbs.size \< 8  
@newthumbs \<\< project  
else  
@everything \<\< project  
end  
end  
@remaining = @everything - @newthumbs # sets a new array for the  
remaining thumbnails  
respond\_to do |format| #AJAX  
format.js if request.xhr?  
format.html {redirect\_to\_index}  
end  
end

My guess is that the app is only reading @newthumbs and @remaining  
arrays within the “index” method. It doesn’t take into account the  
updates that were made to the arrays within the “next” method. How do  
I fix this?

Thank you very much in advance!

---

<div class="post-metadata">

**Author:** ![Adler\_Santos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@Adler\_Santos](https://www.ruby-forum.com/u/Adler_Santos)\
**Post date:** [April 21, 2010, 1:38pm UTC](https://www.ruby-forum.com/t/problem-with-partial-rendering-for-groups-of-objects/187299/2 "2010-04-21T13:38:21Z")

</div>

Errata:

Replace \<% form\_remote\_tag :url =\> {:action =\> “next”, :remaining =\>  
@everything} do %\> with \<% form\_remote\_tag :url =\> {:action =\>  
“next”, :remaining =\> @remaining} do %\>.

---

<div class="post-metadata">

**Author:** ![Adler\_Santos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@Adler\_Santos](https://www.ruby-forum.com/u/Adler_Santos)\
**Post date:** [April 21, 2010, 7:25pm UTC](https://www.ruby-forum.com/t/problem-with-partial-rendering-for-groups-of-objects/187299/3 "2010-04-21T19:25:40Z")

</div>

Adler Santos wrote:

> Errata:
> 
> Replace \<% form\_remote\_tag :url =\> {:action =\> “next”, :remaining =\>  
> @everything} do %\> with \<% form\_remote\_tag :url =\> {:action =\>  
> “next”, :remaining =\> @remaining} do %\>.

Somewhere you need to keep track of which iteration of “Next” you are on  
in the view - perhaps a hidden field would suffice.

A processing heavy version would use in\_groups\_of in the controller to  
let you walk the groups and just return the one you want (along with the  
new current set so you know that on the next ‘Next’ iteration).

---

<div class="post-metadata">

**Author:** ![Adler\_Santos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@Adler\_Santos](https://www.ruby-forum.com/u/Adler_Santos)\
**Post date:** [April 22, 2010, 10:28am UTC](https://www.ruby-forum.com/t/problem-with-partial-rendering-for-groups-of-objects/187299/4 "2010-04-22T10:28:21Z")

</div>

On Apr 22, 1:25 am, Ar Chron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Somewhere you need to keep track of which iteration of “Next” you are on  
> in the view - perhaps a hidden field would suffice.
> 
> A processing heavy version would use in\_groups\_of in the controller to  
> let you walk the groups and just return the one you want (along with the  
> new current set so you know that on the next ‘Next’ iteration).

I looked at the log to have an idea what’s going on after pressing the  
“next” button a couple of times (with nothing happening). It keeps  
reading @remaining containing the 2nd group of projects with all the  
remaining projects after it. This means it doesn’t process the  
“@remaining = @everything - @newthumbs” within the “next” method. Is  
there a line of code I’m missing that will allow the execution of the  
“next” method and will overwrite the initial @remaining that was  
produced by the “index” method? Thanks.

---

<div class="post-metadata">

**Author:** ![Adler\_Santos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@Adler\_Santos](https://www.ruby-forum.com/u/Adler_Santos)\
**Post date:** [April 22, 2010, 2:13pm UTC](https://www.ruby-forum.com/t/problem-with-partial-rendering-for-groups-of-objects/187299/5 "2010-04-22T14:13:37Z")

</div>

Adler Santos wrote:

> Is  
> there a line of code I’m missing that will allow the execution of the  
> “next” method and will overwrite the initial @remaining that was  
> produced by the “index” method? Thanks.

I assume that @remaining from your controller methods is squirreled away  
in a hidden field on your form somewhere.

After a call to ‘Next’, how is @remaining getting updated on the form,  
so that this updated value is available for the next invocation of  
‘Next’?

It doesn’t seem that the new @remaining is pushed back out to the form  
anywhere by your code in next.js.rjs, which means that the updated value  
from the current invocation of ‘next’ disappears into the aether. Your  
next ‘next’ is reusing the original @remaining. I think.
