RJS - checking if element exists?

There are at least two solutions: 1. Create custom javascript methods, e.g.   function create_or_update_element(id, content) {      ....   }

2. Install js-if-blocks plugin   ./script/plugin install svn://rubyforge.org/var/svn/js-if-blocks/trunk/js-if-blocks

and then write your RJS code like this:

  page.if page[my_element_id] do     page.replace_html element_id, content   page.else     page.insert_html :top, parent_id,       "<div id='#{element_id}'>#{content}</div>"   end

Thanks so much for the replies. Am I right in thinking that these plugins will work if the element itself isn't there?

1. Actually, those two references are to the same plugin 2. Yes, the condition will work.

I tried:

page.if page['test'] do   puts "Working!" page.else   puts "Else working!" end

"puts" will not work, because, actually, there is no true "if" statement. Those are just methods that generate proper javascript. So, in your case, you should use something like page.alert('Working!')

Where test does not exist on the page, and I get an error saying If is not defined :os

As far as I understand, you haven't installed plugin yet. The error is because "page.if" method is defined in that plugin and not part of standard Rails distribution.

You can check if you have non-empty js-if-blocks directory under ./vendor/plugins directory.

Also, make sure you have restarted you server.