"#{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?
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.
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 << "}"
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.
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 << "}"
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:
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:
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 ))
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
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.
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')){"