I have been pulling my hair out for way too long on this…and it seems
so trivial! The following does not work:
if page[‘test’].className == “whatever”
page[‘test’].className = “whatever2”
end
Now that I write this post, I realize why it doesn’t work - className is
a value in the Javascript code - not in Rails. But, is there any way to
access the className value in the IF statement?
I have been pulling my hair out for way too long on this…and it seems
so trivial! The following does not work:
if page[‘test’].className == “whatever”
page[‘test’].className = “whatever2”
end
Now that I write this post, I realize why it doesn’t work - className is
a value in the Javascript code - not in Rails. But, is there any way to
access the className value in the IF statement?
Though you can call the block param anything you want, they are always
rendered in the javascript as .value, index… Other enumerations, like
collect() return the last statement:
page.select(‘#items li’).collect(‘hidden’) do |item|
item.hide
end
=> var hidden = $$(‘#items li’).collect(function(value, index) {
Are you trying to update the style class of a specific node or all of
the
nodes containing a particular class?
Is the RJS returned from a specific AJAX function link from the node you
are
interested in? You could use the :with attribute to pass in the style
class
name if this is the case.
I have been pulling my hair out for way too long on this…and it seems
so trivial! The following does not work:
if page[‘test’].className == “whatever”
page[‘test’].className = “whatever2”
end
Now that I write this post, I realize why it doesn’t work - className is
a value in the Javascript code - not in Rails. But, is there any way to
access the className value in the IF statement?
Yeah, there’s a fundamental misunderstanding going on here. When you
write an RJS template, you’re not writing Ruby that communicates ‘live’
with Javascript on the web browser. The entire RJS template is
converted to Javascript on the server, and is then sent in one big lump
to the web browser, where it is run.
Therefore, you cannot do anything in RJS that involves ‘reading’
something about the state of the browser or document. Or in other
words, the entire RJS template has to make sense without any knowledge
of what’s going on in the browser/document.
I’d just drop into Javascript for this kind of thing.
access the className value in the IF statement?
of what’s going on in the browser/document.
I’d just drop into Javascript for this kind of thing.
Yet, there is a solution. I had the very same problems and have written
a
simple plugin that allows something like this:
page.if page[‘test’].hasClassName(‘whatever’) do
page[‘test’].classNames.set(‘whatever’)
end
You can install it with this command:
./script/plugin install
svn://rubyforge.org/var/svn/js-if-blocks/trunk/js-if-blocks
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.