Forum: Ruby on Rails helpers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fb441b0650e84254ccd07224e069c782?d=identicon&s=25 gordon (Guest)
on 2007-07-25 00:10
(Received via mailing list)
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
===================
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2007-07-25 03:18
(Received via mailing list)
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
9a71df6388c8d37bfdf0e18742ea944a?d=identicon&s=25 AndyV (Guest)
on 2007-07-25 19:00
(Received via mailing list)
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
Fb441b0650e84254ccd07224e069c782?d=identicon&s=25 gordon (Guest)
on 2007-08-18 07:32
(Received via mailing list)
Thanks guys. The suggestion worked!

 Gordon
This topic is locked and can not be replied to.