How to check the existence of div?

Hi...

  I have an rjs file to replace a div using ajax call.

  page["#{params[:id]}parents"].replace :partial => 'cipart/expanded_item', :locals => { :expand_items => CiRelation.find_all_by_ci_id(params[:id])}

  "#{params[:id]}parents" is the name of the div to be replaced.

  In some cases depending on a condition, this div does not exist. Then in the rjs file I have to check for the existence of this div. How can I do this?

Please help.

Thanks Suneeta

if page[“#{params[:id]}parents”]

Ryan Bigg wrote:

if page["#{params[:id]}parents"]

>

-- Appreciated my help? Reccommend me on Working With Rails http://workingwithrails.com/person/11030-ryan-bigg

Hi   Thanks for replying.

  I tried this if condition. But an rjs error message is displayed.   'Type error: $("15parents") has no properties'   15parents is the name of the div. 15 is obtained by evaluating params[:id]   Please help.

Suneeta

Try suffixing it with a call to .nil?

Try suffixing it with a call to .nil?

That fundamentally cannot work. that if is a ruby if (ie evaluated
server side), but the condition is a client side one (whether a
certain element is present).

what does work is

page << "if $('some_id'){" #do some other stuff page << "}"

There is rails plugin that helps with this http://rubyforge.org/projects/js-if-blocks/

Bear in mind that the partials are still rendered etc. no matter what.
The only thing that is conditional is whether or not you use the chunk
of html you've ferried over to the browser.

Fred

Ryan Bigg wrote:

Try suffixing it with a call to .nil?

>>

>

-- Appreciated my help? Reccommend me on Working With Rails http://workingwithrails.com/person/11030-ryan-bigg

I tried suffixing the if with .nil? But it didn't work. Any other solution?

Frederick Cheung wrote:

Frederick Cheung wrote:

Try suffixing it with a call to .nil?

That fundamentally cannot work. that if is a ruby if (ie evaluated server side), but the condition is a client side one (whether a certain element is present).

what does work is

page << "if $('some_id'){" #do some other stuff page << "}"

There is rails plugin that helps with this http://rubyforge.org/projects/js-if-blocks/

Bear in mind that the partials are still rendered etc. no matter
what. The only thing that is conditional is whether or not you use the
chunk of html you've ferried over to the browser.

Fred

Can I do this checking without using any plugin? I tried if $('some_id'). I think the problem is, in my case the id of div is obtained by evaluating #{params[:id]}

You don't need the plugin, you can just use what i posted above , it's
just a bit neeter with the plugin. if your div id is not static that
is not a problem:

page << "if $('#{ "foo" + "bar" + "baz"}'){"

Fred

Frederick Cheung wrote:

what does work is The only thing that is conditional is whether or not you use the
chunk of html you've ferried over to the browser.

Fred

Can I do this checking without using any plugin? I tried if $('some_id'). I think the problem is, in my case the id of div is obtained by evaluating #{params[:id]}

You don't need the plugin, you can just use what i posted above , it's just a bit neeter with the plugin. if your div id is not static that is not a problem:

page << "if $('#{ "foo" + "bar" + "baz"}'){"

Fred

I tried the following: page << "if $('#{params[:id]}parents'){" page["#{params[:id]}parents"].replace :partial => 'cipart/expanded_item', :locals => { :expand_items => CiRelation.find_all_by_ci_id(params[:id]), :relation => 'parents', :ci_id=>params[:ci_id] } page << "}"

If div exists, the partial file is executed. But it is not replaced and the result is not displayed.

I tried the following: page << "if $('#{params[:id]}parents'){"

page["#{params[:id]}parents"].replace :partial => 'cipart/expanded_item', :locals => { :expand_items => CiRelation.find_all_by_ci_id(params[:id]), :relation => 'parents', :ci_id=>params[:ci_id] } page << "}"

If div exists, the partial file is executed. But it is not replaced and the result is not displayed.

You've probably got the wrong div id. Poke around in firebug.

Fred

Frederick Cheung wrote:

But have you checked that the div actually has that id? Does the generated javscript look sane ?

Fred

Frederick Cheung wrote:

Frederick Cheung wrote:

CiRelation.find_all_by_ci_id(params[:id]), :relation => 'parents',

But still that div is not replaced. Don't know why.

But have you checked that the div actually has that id? Does the generated javscript look sane ?

Fred

Yes I checked the generated html code. The div has correct id.

Whack a breakpoint in your javascript and follow what happens (because
you're using rjs you'll have to stick the breakpoint in prototype
first (evalResponse is probably a good place ))

Fred

Perhaps not for beginners but at some point in my rails development work I realized that complex rjs files can be more trouble than they are worth. For one, it is basically just masking javascript with ruby for no good reason frankly and doing anything non-trivial (if statements, loops etc) are generally easier in javascript anyways. How do you rectify this then?

Two ways...simple way is simply write the function in pure javascript and then invoke it in the rjs...i.e page.call "someFunction", "param1", "param2"

but what if I REALLY want to be able to dynamically evaluate ruby code as part of the template....I have used a plugin called MinusMOR

http://agilewebdevelopment.com/plugins/minusmor

Basically allows you to write javascript and embed ruby much the same way as writing html and embedding ruby in erb.

Anyways, just something to think about. Javascript is not evil (in my opinion) and often it is actually easier just to bite the bullet and use it directly.

Frederick Cheung wrote:

I tried the following: page << "if $('#{params[:id]}parents'){"

If you'd checked the generated javascript you would have seen that
there's an obvious syntax error here. It should be page << "if($('#{params[:id]}parents')){"

Fred

Frederick Cheung wrote: