[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Rails AJAX question

voice10@gmail.com

2/10/2006 4:49:00 AM

Hi all, It must be a very simple solution, but i'm just not getting it.
I want to display some picture continously on a page using Rail+AJAX.
Here is the snippet of code:

Controller
------------------------
def show
@albums = Album.find(:all)
@size = @albums.length
end

def show_next
@id = params[:cur_id].to_i
@size = params[:max].to_i
if ( @id == 0 or ir @id > @size)
@id = 1
end

@album = Album.find(@id)
@id += 1
end

show.rhtml
-------------------------------------
<html>
<body>
Continous picture display<br>
<%= periodically_call_remote(:update => 'mydiv',
:url => { :action => :show_next,
:cur_id => @id, :max => @size},
:frequency => 2 )
<div id="mydiv">
image will display here
</div>
</body>
</html>

show_next.rhtml
-------------------------------------

def show_next
<img width="600" height="200" src="<%= @album .image_url %>"/>
end

------------------------------------------------------------------------------------------------------------------

But the @id is not being updated. I think I need to store the @id
statically in the html page (may as hidden field), but is there any
better solution for this

thanks in avdvance
/Rick

2 Answers

tsumeruby

2/10/2006 5:15:00 AM

0

On Friday 10 February 2006 01:53 pm, voice10@gmail.com wrote:

EWRONGMAILINGLIST

http://lists.rubyonrails.org/mailman/list...

Tsume

> Hi all, It must be a very simple solution, but i'm just not getting it.
> I want to display some picture continously on a page using Rail+AJAX.
> Here is the snippet of code:
>
> Controller
> ------------------------
> def show
> @albums = Album.find(:all)
> @size = @albums.length
> end
>
> def show_next
> @id = params[:cur_id].to_i
> @size = params[:max].to_i
> if ( @id == 0 or ir @id > @size)
> @id = 1
> end
>
> @album = Album.find(@id)
> @id += 1
> end
>
> show.rhtml
> -------------------------------------
> <html>
> <body>
> Continous picture display<br>
> <%= periodically_call_remote(:update => 'mydiv',
>
> :url => { :action => :show_next,
> :
> :cur_id => @id, :max => @size},
> :
> :frequency => 2 )
>
> <div id="mydiv">
> image will display here
> </div>
> </body>
> </html>
>
> show_next.rhtml
> -------------------------------------
>
> def show_next
> <img width="600" height="200" src="<%= @album .image_url %>"/>
> end
>
> ---------------------------------------------------------------------------
>---------------------------------------
>
> But the @id is not being updated. I think I need to store the @id
> statically in the html page (may as hidden field), but is there any
> better solution for this
>
> thanks in avdvance
> /Rick


anne001

2/10/2006 12:21:00 PM

0

I liked this tutorial, it has a nice example on the lack of memory of
web applications
http://www.troubleshooters.com/codecorn/ruby/rails/rapidlearning...

I have been working with a controller code as such
if params.has_key? :id
@laststimobj=Stimuli.find(params[:id])
@stimuliobj=Stimuli.find(params[:id].to_i + 1)
else
@stimuliobj = Stimuli.find(:first)
end

with a form in the view
<%= start_form_tag(:action => "hello") %>
<%= hidden_field_tag "id", @stimuliobj.id %>
<p>
Name:
<%= text_field("responseobj", "name", "size" => "20") %>
<%= submit_tag("Name") %>
<% end_form_tag %>

you can use
render_text params.inspect
in your show_next method to see what your parameter hash is like. I am
not sure if an action redirect can transmit parameters.