Forum: Ruby on Rails Question about periodically_call_remote

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jeff C. (Guest)
on 2005-12-31 04:32
I'm not sure how to ask this succinctly, so bear with me.  I'm trying to
learn how to use periodically_call_remote to update a div.

I have a table whose rows have the data I want to display in the div,
one at a time, every 10 seconds (let's say).  For simplicity let's
pretend each row just has one column, "description," which is just a
text column.

I have a partial, let's say it's called _currentdescription.rhtml, that
just shows the description in a p tag.

The table row to show in the view is initially determined by the
controller's index action, and subsequent calls go through
next_description:

def index
  @description_index = 0
  @description = get_current_description
end

# Called by ajax call; renders the partial directly after
# determining next row to show
def next_description
  @allrows = find_all
  @description_index = params[:oldindex] + 1
  # if we've shown all, then start over again
  if (@description_index > @allrows.length)
    @description_index = 0
  end
  @description = get_current_description
  render :partial => 'currentdescription'
end

def get_current_description
  @allrows[@description_index].description
end

Ok, now the index.rhtml fragment looks like:

<div id="info">
  <%= render :partial => 'currentdescription' %>
  ,%= periodically_call_remote(:update => 'info', :url => { :action =>
'next_description', :oldindex => @description_index })
</div>

Whew!  Thanks for staying with me this far.

Now, the behavior I'm seeing is that the first row's description is
shown, and then ten seconds later the second one appears (yay!).  But it
never changes after that.

The problem is that the javascript generated by periodically_call_remote
is never updated so it's always calling with :oldindex = 0.

How can I have the periodically_call_remote "know" about the new value
of @description_index that I've assigned from the "next_description"
call?

The only way around this that I can think of is to keep track of the
index server-side instead of client-side, but I really want to avoid
having to manage user sessions if possible.

Thanks!
Jeff
This topic is locked and can not be replied to.