# How do you pass html objects from the view to the helper cla

**URL:** <https://www.ruby-forum.com/t/how-do-you-pass-html-objects-from-the-view-to-the-helper-cla/88248>\
**Category:** Rails\
**Created:** [March 9, 2007, 5:29am UTC](https://www.ruby-forum.com/t/how-do-you-pass-html-objects-from-the-view-to-the-helper-cla/88248 "2007-03-09T05:29:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 9, 2007, 5:29am UTC](https://www.ruby-forum.com/t/how-do-you-pass-html-objects-from-the-view-to-the-helper-cla/88248/1 "2007-03-09T05:29:00Z")

</div>

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

I have in the view

#### test

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 9, 2007, 5:37am UTC](https://www.ruby-forum.com/t/how-do-you-pass-html-objects-from-the-view-to-the-helper-cla/88248/2 "2007-03-09T05:37:01Z")

</div>

removed\_email\_address@domain.invalid wrote:

> 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.

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 9, 2007, 5:57am UTC](https://www.ruby-forum.com/t/how-do-you-pass-html-objects-from-the-view-to-the-helper-cla/88248/3 "2007-03-09T05:57:02Z")

</div>

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 …, but I don’t know how to show  
it or know how to check to see if it is hidden using the helper class.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 9, 2007, 6:14am UTC](https://www.ruby-forum.com/t/how-do-you-pass-html-objects-from-the-view-to-the-helper-cla/88248/4 "2007-03-09T06:14:03Z")

</div>

removed\_email\_address@domain.invalid 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 …, 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.

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 9, 2007, 6:22am UTC](https://www.ruby-forum.com/t/how-do-you-pass-html-objects-from-the-view-to-the-helper-cla/88248/5 "2007-03-09T06:22:04Z")

</div>

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

On Mar 9, 12:55 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 9, 2007, 6:46am UTC](https://www.ruby-forum.com/t/how-do-you-pass-html-objects-from-the-view-to-the-helper-cla/88248/6 "2007-03-09T06:46:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 9, 2007, 7:06am UTC](https://www.ruby-forum.com/t/how-do-you-pass-html-objects-from-the-view-to-the-helper-cla/88248/7 "2007-03-09T07:06:07Z")

</div>

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]](http://www.oreilly.com/catalog/jscript4/)
- [Dynamic HTML: The Definitive Reference [Book]](http://www.oreilly.com/catalog/dhtmlref/)

On Mar 9, 1:45 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
