def hide_and_reset(div)
update_page do |page|
if page[div].title == 'show'
page[div].hide
page[div].title = 'hide'
else
page[div].title = 'show'
page[div].show
end
end
end
def hide_and_reset(div)
update_page do |page|
if page[div].title == 'show'
page[div].hide
page[div].title = 'hide'
else
page[div].title = 'show'
page[div].show
end
end
end
You can't. The helper has already been run on the server side before the page is sent to the client. At that point the browser has no idea that there was a helper function in the original .rhtml file.
To hide or show DIVs on the browser you need to write some JavaScript or if the hide/show conditions depend on information on the server you can use Ajax.
The helper class seems to update the view. I can hide <div
id="select02_div" title="show">...</div>, but I don't know how to show
it or know how to check to see if it is hidden using the helper class.
The helper class seems to update the view. I can hide <div
id="select02_div" title="show">...</div>, but I don't know how to show
it or know how to check to see if it is hidden using the helper class.
The hide_and_reset() in the link_to_function looks like a JavaScript function, not a Rails helper method.
In this case, your helper function is actually returning a chunk of
javascript that is called when you click on the image. Ruby's not
changing the page -- javascript is. update_page is generating this
javascript based on the block you gave it. Unfortunately, in this
case, the javascript you're getting is not what you expect. If you
add <%= hide_and_reset(:select02_div, :select01_div) %> you can see
what's being created on your behalf.
update_page() is good in a lot of simple cases, but if you really need
flexibility, you'll have to dip down into native javascript.
For this case something like the following will work
def hide_and_reset(div)
update_page do |page|
el = page[div]
page << <-"end"
if (#{el}.title == "show") { #{el}.hide(); #{el}.title =
"hide"; }
else { #{el}.show(); #{el}.title = "show"; }
end
end
end
Thanks guys. I see your point in not wasting server calls just for
simple stuff that should be done on the client only in plain
javascript. I use the Oreilly book Ruby on Rails Up and Running. It's
too basic. I was wondering if you guys have any good books or
tutorials you recommend that provides more advance use of ruby on
rails, especially with AJAX.