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

and then write your RJS code like this:

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

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!"
  puts "Else working!"

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