# Simple Rendering Question

**URL:** <https://www.ruby-forum.com/t/simple-rendering-question/47764>\
**Category:** Rails\
**Created:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/simple-rendering-question/47764 "2005-11-12T12:49:53Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dylans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@dylans](https://www.ruby-forum.com/u/dylans)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/simple-rendering-question/47764/1 "2005-11-12T12:49:53Z")

</div>

I have a simple eRb snippet that get’s a hash via an objects method  
call,  
and populates a Javascript call… for example:

\<%= @hash = asdf.getHash() %\>

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 !

---

<div class="post-metadata">

**Author:** ![dylans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@dylans](https://www.ruby-forum.com/u/dylans)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/simple-rendering-question/47764/2 "2005-11-12T12:49:53Z")

</div>

Dylan S. wrote:

> \<%= @hash = asdf.getHash() %\> 
> 
> 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'] -%>' );
```

---

<div class="post-metadata">

**Author:** ![dylans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@dylans](https://www.ruby-forum.com/u/dylans)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/simple-rendering-question/47764/3 "2005-11-12T12:49:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dylans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@dylans](https://www.ruby-forum.com/u/dylans)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/simple-rendering-question/47764/4 "2005-11-12T12:49:54Z")

</div>

The parameter for the Javascript Object is a number. I tried doing @  
stuff.to\_i and @stuff.to\_s, but no luck.

---

<div class="post-metadata">

**Author:** ![dylans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@dylans](https://www.ruby-forum.com/u/dylans)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/simple-rendering-question/47764/5 "2005-11-12T12:49:54Z")

</div>

Dylan S. 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?

---

<div class="post-metadata">

**Author:** ![dylans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@dylans](https://www.ruby-forum.com/u/dylans)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/simple-rendering-question/47764/6 "2005-11-12T12:50:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dylans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@dylans](https://www.ruby-forum.com/u/dylans)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/simple-rendering-question/47764/7 "2005-11-12T12:50:09Z")

</div>

Anybody have any ideas ? Just trying to pass a parameter into a  
Javascript  
method. Simple stuff, but it seems impossible !

---

<div class="post-metadata">

**Author:** ![dylans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@dylans](https://www.ruby-forum.com/u/dylans)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/simple-rendering-question/47764/8 "2005-11-12T12:50:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dylans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@dylans](https://www.ruby-forum.com/u/dylans)\
**Post date:** [November 13, 2005, 5:25am UTC](https://www.ruby-forum.com/t/simple-rendering-question/47764/9 "2005-11-13T05:25:57Z")

</div>

Dylan S. 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} );” -%\>

---

<div class="post-metadata">

**Author:** ![dylans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@dylans](https://www.ruby-forum.com/u/dylans)\
**Post date:** [November 12, 2005, 10:31pm UTC](https://www.ruby-forum.com/t/simple-rendering-question/47764/10 "2005-11-12T22:31:23Z")

</div>

it will work if you wait until the page has loaded before calling the  
javascript function:

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

---

<div class="post-metadata">

**Author:** ![dylans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/90ced4/32.png) [@dylans](https://www.ruby-forum.com/u/dylans)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/simple-rendering-question/47764/11 "2005-11-12T12:49:59Z")

</div>

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