How do you pass html objects from the view to the helper classes?

I would like to pass the title to the helper class and manipulate it.

I have in the view

<div id="select02_div" title="show">   <h4>test</h4> </div>

In the helper, I have

  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

jimjohnlists@yahoo.com wrote:

I would like to pass the title to the helper class and manipulate it.

I have in the view

<div id="select02_div" title="show">   <h4>test</h4> </div>

In the helper, I have

  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.

Thanks. I can call the helper with this line in my code in the view.

<%= link_to_function image_tag("graphics/ select04.gif", :alt=>'Programming', :size=>'100x20', :border=>0), hide_and_reset(:select02_div, :select01_div) %>

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.

jimjohnlists@yahoo.com wrote:

Thanks. I can call the helper with this line in my code in the view.

<%= link_to_function image_tag("graphics/ select04.gif", :alt=>'Programming', :size=>'100x20', :border=>0), hide_and_reset(:select02_div, :select01_div) %>

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.

For most things you don't actually need ajax -- instead you can usually get away with plain javascript. The ORA javascript and dhtml books are good starts: * JavaScript: The Definitive Guide, Fourth Edition [Book] * Dynamic HTML: The Definitive Reference [Book]