# Helpers

**URL:** <https://www.ruby-forum.com/t/helpers/110294>\
**Category:** Rails\
**Created:** [July 25, 2007, 12:10am UTC](https://www.ruby-forum.com/t/helpers/110294 "2007-07-25T00:10:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gordon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3da27b/32.png) [@gordon](https://www.ruby-forum.com/u/gordon)\
**Post date:** [July 25, 2007, 12:10am UTC](https://www.ruby-forum.com/t/helpers/110294/1 "2007-07-25T00:10:00Z")

</div>

I have the following helper (see below) with two calls to  
observe\_field. In the viewer, the helper is used  
according to:

\<%= observe\_slider ‘time’, 0.1, ‘display\_full\_size’, “slider\_val”,  
u[1] %\>

Only the second call to observe\_field is acted up. If I invert the  
order of the calls in the helper,  
then it is still the second call to observe\_field that works. It is as  
if the second call was overwriting  
the first. I found a similar behavior in the viewer before using the  
helper. If I wrapped each call  
to observe\_field in \<%= %\> tags, they both worked. However, if I  
wrapped both calls into a single  
\<%= %\>, only the second one worked. Does any on of you have any  
ideas?

Thanks,

Gordon

=====================  
Helper:

def observe\_slider(id, freq, img\_id, local\_var, url)  
# :idd variable name is hardcoded. Should not be.  
observe\_field( “#{id}\_h”,  
:frequency =\> freq,  
:update =\> “#{img\_id}\_hide”,  
:with =\> local\_var,  
:complete =\> “update\_time(request)”,  
:url =\> { :action =\> “#{img\_id}”, :idd =\> url })

```
# I cannot have two observe_field statements. The second one
# seems to overwrite the first one. Do now know why
# WHY, WHY!!!

observe_field( "#{id}",
     :update => "#{img_id}_hide",
     :with => local_var,
     :complete => 'update_time(request)',
   :url => { :action => "#{img_id}", :idd => url } )

```

# end

---

<div class="post-metadata">

**Author:** ![gordon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3da27b/32.png) [@gordon](https://www.ruby-forum.com/u/gordon)\
**Post date:** [July 25, 2007, 3:18am UTC](https://www.ruby-forum.com/t/helpers/110294/2 "2007-07-25T03:18:53Z")

</div>

i believe because observe\_field writes to the output stream, you need  
to capture its output like

def observe\_slider(id, freq, img\_id, local\_var, url)  
# :idd variable name is hardcoded. Should not be.  
str = observe\_field( “#{id}\_h”,  
:frequency =\> freq,  
:update =\> “#{img\_id}\_hide”,  
:with =\> local\_var,  
:complete =\> “update\_time(request)”,  
:url =\> { :action =\> “#{img\_id}”, :idd =\> url })

```
str << observe_field( "#{id}",
     :update => "#{img_id}_hide",
     :with => local_var,
     :complete => 'update_time(request)',
   :url => { :action => "#{img_id}", :idd => url } )
return str

```

end

---

<div class="post-metadata">

**Author:** ![gordon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3da27b/32.png) [@gordon](https://www.ruby-forum.com/u/gordon)\
**Post date:** [July 25, 2007, 7:00pm UTC](https://www.ruby-forum.com/t/helpers/110294/3 "2007-07-25T19:00:12Z")

</div>

I think you’re on the right track. The issue appears to be that the  
helper is just like any other method call – you’re going to receive  
back whatever the method returns and it’s results are then going to be  
added to the page. In the original code the return value, since it’s  
not explicitly stated, will be the result of the last method call  
which is always the last observe\_field call. That’s why it appears  
that the last call is “overwriting” the previous call(s).

@jemminger’s recommendation will work because observe\_field will  
return a string and when you concat the strings you can return the  
cumulative result and then render that into the page.

AndyV

---

<div class="post-metadata">

**Author:** ![gordon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3da27b/32.png) [@gordon](https://www.ruby-forum.com/u/gordon)\
**Post date:** [August 18, 2007, 7:32am UTC](https://www.ruby-forum.com/t/helpers/110294/4 "2007-08-18T07:32:21Z")

</div>

Thanks guys. The suggestion worked!

Gordon
