Conditional Protoypte updates - Is there a simple answer?

I have a simple show/hide function for a div as follows :

<%= link_to_function "Show|Hide",
    visual_effect(:toggle_slide, :images, :duration => 1, :queue => 

‘end’) %>

I’d like to just display Hide, then when the link is clicked perform the
toggle_slide and then change the link text to ‘Show’ (and back when
clicked again).

I’d like to do something like :

<%= link_to_function "Hide", update_page{|page|
page.visual_effect(:toggle_slide, :images, :duration => 1, :queue => 

‘end’)
if page.select(‘toggle_images’) == ‘Hide’
page.select(‘toggle_images’).replace_html(:text => ‘Show’)
else
page.select(‘toggle_images’).replace_html(:text => ‘Hide’)
end
},
{ :id => ‘toggle_images’} %>

However you can’t use Ruby within the block like this (as far as I can
tell). I could use an Ajax call to a partial to do the replace but this
seems overkill.

Does anybody have a solution for this example that doesn’t involve
custom JS or an Ajax call ?

I think this code is just screaming for an RJS template.

Change your link_to_function to a link_to_remote and name your action
after
your RJS template name. Don’t define a method for this in your
controller
and Rails will magically find your RJS template. All of your page.*
code
should go into the RJS template just fine.

Read about RJS stuff here:

http://rubynoob.com/articles/2006/05/13/simple-rails-rjs-tutorial

And here’s a list to lots of stuff:

http://www.rubyinside.com/16-rjs-resources-and-tutorials-for-rails-programmers-5.html

Hope that helps!

On 9/26/06, robl [email protected] wrote:

clicked again).
end

Posted via http://www.ruby-forum.com/.


Terry (TAD) Donaghe

http://www.tadspot.com

But that means I need to do a remote call to a server to replace some
innerHTML for a DOM element for an already known value - not very
efficient really …

You might want to take a look at the UJS plugin.

http://www.ujs4rails.com/

This seems like the kind of thing you want. It’s all ruby, with no
javascript anywhere. And can be DRY too if used well.

Have a Hide, and your RJS-ish code inside an
apply_behavior.

Vish