Wednesday, June 14, 2006

RJS Rails If statement

If you do something like:

page.replace_html 'non_existent', 'some text'

you get a Javascript error and sequent lines are not processed. The first idea to solve this problem would be:

if page['not_valid']
    page.replace_html 'not_valid', 'some text'
end

However, you don't get what you meant. Not at all. This is translated to

$("not_valid")
Element.update("not_valid", "some text");

The best way I found do express that meaning is

page.select('not_valid').each do |unique|
    page.replace_html unique, 'some text'
end

That works.

No comments: