How to conditionally replace an element using RJS template

I have an RJS template that looks like this:

page.replace “id_#{@foo.id}”, :partial => “row”

But I only want it to execute if “id_#{@foo.id}” is an element on the
current page?

something like:

if page.exists?(“id_#{@foo.id}”)
page.replace “id_#{@foo.id}”, :partial => “row”
end

I have read through every API page that I can find and can’t find such
a beast? What would be the best way to do this?

Thanks

Mikel

not sure, but try this:

page << “if $(‘id_#{@foo.id}’) {”
page.replace “id_#{@foo.id}”, :partial => “row”
page << “}”

you can feed straight Javascripts into the page object with << (as
documented in the API), so i think it should work

Thanks for the pointer!

You were almost there, the solution was:

page << “elem = $(‘id_(#{@foo}’);”
page << “if (elem) {”
page.replace “id_(#{@foo}”, :partial => “row”
page << “}”

Needed the extra step.

Thanks :slight_smile:

Mikel

On 7/1/07, Mikel [email protected] wrote:

Needed the extra step.

Thanks :slight_smile:

Mikel

You shouldn’t need an extra step. Just some brackets.

page << “if( $(‘id_#{@foo.id id_#[email protected]}’) ){”
page.replace “id_#{@foo.id id_#[email protected]}”, :partial => “row”
page << “}”

That should be the equivalent of what you had.

Daniel

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