Problem with partial rendering for groups of objects

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!

Errata:

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

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).

On Apr 22, 1:25 am, Ar Chron [email protected] 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.

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.