Topic: Conditional RJS code

Hi,
I have the following code:

<%= link_to_function "Test" do |page|
    page[:edit_area].visual_effect :slide_down
end
%>

What I want, is that the edit_area div only slides if it's not already visible.
How can I accomplish that?

I've tried something along the lines of:

if page.visible('edit_area')

but it doesn't work. Any hints?

Thanks.

Re: Conditional RJS code

"if" conditions are tricky to do in RJS because it is just generating javascript code which later on gets interpreted by the browser. While the server is interpreting Ruby it can't tell whether an element is visible or not.

To solve this you need to do the condition in javascript like this:

page << "if (1 == 1) {"
page[:edit_area].visual_effect :slide_down
page << "}"

Of course replace the if condition with whatever you want. I mention this in the RJS Tips Railscasts episode.

Railscasts - Free Ruby on Rails Screencasts