Verifying existence of elements

My controller ends like this:

render :update do |page|
  page.replace_html 'someDiv', :partial => 'somePartial'
end

I need to edit this to verify that someDiv exists before I go
replacing_htmling it. I’m at a loss to figure out if this can be
done. Any ideas?

Thanks!

On 8/31/07, Josh [email protected] wrote:

My controller ends like this:

render :update do |page|
  page.replace_html 'someDiv', :partial => 'somePartial'
end

I need to edit this to verify that someDiv exists before I go
replacing_htmling it. I’m at a loss to figure out if this can be
done. Any ideas?

are you not in control of the content of your pages? As far as I
know, the only way to do something like this would be to read the
entire page and search to make sure the div exists before continuing.
Unfortunately you can’t just execute the rjs call and expect it to
throw an exception on error (the exception is thrown outside the
context of rails and handled directly by the prototype library)

You can use the following library to allow you to parse your html files:

but of course it’ll be faster when operating on local files than
through using open-uri. But this is an expensive operation which you
really want to avoid if possible.

Adam

Parse the entire page with hpricot? That’s rather silly. You can use
basic javascript or the prototype library to check for the existance
of an element with an id, and you can use use the concatenation
operator to add plain javascript to the output:

page << ‘if (${‘someDiv’)) {’
page.replace_html ‘someDiv’, :partial => ‘somePartial’
page << ‘}’

It’s not the most elegant solution because you’re not writing pure
ruby any more, but it’s quick and simple if you know a little
javascript.

Rein

Whoops, typo:

page << ‘if (${‘someDiv’)) {’

should be:

page << ‘if ($(‘someDiv’)) {’

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs