Forum: Ruby on Rails Simple Rendering Question

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.
Ff82af3238a57fbd1212832ec1a19f28?d=identicon&s=25 dylans (Guest)
on 2005-11-12 12:49
(Received via mailing list)
I have a simple eRb snippet that get's a hash via an objects method
call,
and populates a Javascript call... for example:

<head>
<script src="http://www.asdf.com/asdfasfd.js"></script>
</head>
<body>
<%= @hash = asdf.getHash() %>
<script>
blah.doSomething(<%= @hash['blah'] %>);
</script>
</body>

When I view the source of the resulting HTML, the doSomething() method
is
indeed populated with the correct value from the hash... however, the
actual
method get's called "without" the value.

So, I'm assuming that there is some reason the Javascript is being
rendered
and executed prior to the eRb code.
Is this correct ?

Any insight into this would be appreciated !
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 mrj (Guest)
on 2005-11-12 12:49
(Received via mailing list)
Dylan Stamat wrote:

> <body>
>     <%= @hash = asdf.getHash() %>
>     <script>
>            blah.doSomething(<%= @hash['blah'] %>);
>     </script>
> </body>
>
> When I view the source of the resulting HTML, the doSomething() method
> is indeed populated with the correct value from the hash... however, the
> actual method get's called "without" the value.

Should the hash value be quoted?

    blah.doSomething( '<%= @hash['blah'] -%>' );
Ff82af3238a57fbd1212832ec1a19f28?d=identicon&s=25 dylans (Guest)
on 2005-11-12 12:49
(Received via mailing list)
I'm actually extracting the values from the hash above the script... so:

# Get the Hash from the Method
<% @location_hash = @listing.getData %>

# Assign "@stuff" the value of the stuff value in the Hash
<% @stuff = @location_hash['stuff'] %>

# Use "@stuff" in the Javascript
new RandomJavascriptObject(<%= @stuff %>);

The catch is, if I hardcode the value of @stuff in the actual HTML, and
load
the page... everything works fine.
If I use eRb to do the printing like above, the value is actually
printed in
the HTML, but the actual value passed to the Javascript is empty.
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 mrj (Guest)
on 2005-11-12 12:49
(Received via mailing list)
Dylan Stamat wrote:

> # Use "@stuff" in the Javascript
> new RandomJavascriptObject(<%= @stuff %>);
>
> The catch is, if I hardcode the value of @stuff in the actual HTML, and
> load the page... everything works fine.
> If I use eRb to do the printing like above, the value is actually
> printed in the HTML, but the actual value passed to the Javascript is empty.

What type of JavaScript object is the parameter? Number, string, or
other?
Ff82af3238a57fbd1212832ec1a19f28?d=identicon&s=25 dylans (Guest)
on 2005-11-12 12:49
(Received via mailing list)
The parameter for the Javascript Object is a number. I tried doing @
stuff.to_i and @stuff.to_s, but no luck.
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 mrj (Guest)
on 2005-11-12 12:49
(Received via mailing list)
Dylan Stamat wrote:
> The parameter for the Javascript Object is a number.   I tried doing
> @stuff.to_i and @stuff.to_s, but no luck.

Hmm, all I can think of trying is to change the closing tag
from "%>" to "-%>" to keep the closing parenthesis of the
function call on the same line.
Ff82af3238a57fbd1212832ec1a19f28?d=identicon&s=25 dylans (Guest)
on 2005-11-12 12:50
(Received via mailing list)
Hmm.... no luck. I'm baffled :)

The HTML source has the function and the parameters on the same line:
new BlahObject(1234, 1234);

... and if I hardcode the parameters to replicate the above, everything
works out perfectly.

So, I'd have to assume that this is a "Javascript loading before eRb"
issue... or something similar.
I'll keep trying different options to see if I can't nail this down.
Thanks for you help thus far !
==
Dylan
Ff82af3238a57fbd1212832ec1a19f28?d=identicon&s=25 dylans (Guest)
on 2005-11-12 12:50
(Received via mailing list)
I just tried doing a custom "render", and also put the Javascript call
with
hardcoded values in a file to "render_partial"... but no luck.
WTF.
Ff82af3238a57fbd1212832ec1a19f28?d=identicon&s=25 dylans (Guest)
on 2005-11-12 12:50
(Received via mailing list)
Anybody have any ideas ? Just trying to pass a parameter into a
Javascript
method. Simple stuff, but it seems impossible !
Ce60c4f78a63b0695e4dafc4bd7964f7?d=identicon&s=25 vanek (Guest)
on 2005-11-12 22:31
(Received via mailing list)
it will work if you wait until the page has loaded before calling the
javascript function:

<body onload="myjavascriptfunction('<%= @some_msg %>')">

it works for me, but not, as you have pointed out, when you try to call
javascript
before the page has finished loading.

 >script/about
About your application's environment
Ruby version                 1.8.3 (i386-cygwin)
RubyGems version             0.8.11
Rails version                0.14.3
Active Record version        1.13.0
Action Pack version          1.11.0
Action Web Service version   0.9.3
Action Mailer version        1.1.3
Active Support version       1.2.3

I have noticed that for some reason beyond my understanding javascript
functions work
more consistently when placed either in the HEAD or "included" within
the HEAD (not w/in the BODY).
And some of the most common javascript errors are caused by race
conditions (e.g. page loading),
which is why I always wait for the page to load before calling any
javascript.

-lv
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 mrj (Guest)
on 2005-11-13 05:25
(Received via mailing list)
Dylan Stamat wrote:
> Anybody have any ideas ?  Just trying to pass a parameter into a
> Javascript method.  Simple stuff, but it seems impossible !

Perhaps your web server is delivering the function name
and parameters in different response chunks because of
possible long delays in erb output, and your web browser
is not waiting for all the chunks before evaluating the
script section. Try generating the whole script section in erb:

  <%= javascript_tag "new RandomJavascriptObject( #{@stuff} );" -%>
This topic is locked and can not be replied to.