# Newbie in ROR need help about its javascript

**URL:** <https://www.ruby-forum.com/t/newbie-in-ror-need-help-about-its-javascript/134473>\
**Category:** Rails\
**Created:** [April 11, 2008, 7:14am UTC](https://www.ruby-forum.com/t/newbie-in-ror-need-help-about-its-javascript/134473 "2008-04-11T07:14:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![adi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@adi](https://www.ruby-forum.com/u/adi)\
**Post date:** [April 11, 2008, 7:14am UTC](https://www.ruby-forum.com/t/newbie-in-ror-need-help-about-its-javascript/134473/1 "2008-04-11T07:14:06Z")

</div>

hello, i’m a newbie using ROR.  
i have this function below inside my public/application.js and i’m  
wondering whether i can replace “San Fransisco” and “New York” in this  
command “setDirections(“San Francisco”, “New York”, “en\_US”);” with  
session[:address1] and session[:address2]…???  
Please can anyone show me how to do it…  
"  
function initialize() {  
if (GBrowserIsCompatible()) {  
alert(“kyaaaaaaaa”)

```
    map = new GMap2(document.getElementById("map_canvas"));
    gdir = new GDirections(map,

```

document.getElementById(“directions”));  
GEvent.addListener(gdir, “load”, onGDirectionsLoad);  
GEvent.addListener(gdir, “error”, handleErrors);  
setDirections(“San Francisco”, “New York”, “en\_US”);  
}  
}  
"

---

<div class="post-metadata">

**Author:** ![adi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@adi](https://www.ruby-forum.com/u/adi)\
**Post date:** [April 11, 2008, 9:18am UTC](https://www.ruby-forum.com/t/newbie-in-ror-need-help-about-its-javascript/134473/2 "2008-04-11T09:18:33Z")

</div>

On Fri, Apr 11, 2008 at 3:13 PM, Adi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hello, i’m a newbie using ROR.  
> i have this function below inside my public/application.js and i’m  
> wondering whether i can replace “San Fransisco” and “New York” in this  
> command “setDirections(“San Francisco”, “New York”, “en\_US”);” with  
> session[:address1] and session[:address2]…???  
> Please can anyone show me how to do it…

One way (not the DRYest or most maintainable, but definately the  
simplest and it will get you going) is in your view.html.erb file you  
can put:

\<% if session[:address1] -%\>

\<% end -%\>

And then take it out of application.js

Regards

Mikel

---

<div class="post-metadata">

**Author:** ![adi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@adi](https://www.ruby-forum.com/u/adi)\
**Post date:** [April 11, 2008, 9:46am UTC](https://www.ruby-forum.com/t/newbie-in-ror-need-help-about-its-javascript/134473/3 "2008-04-11T09:46:12Z")

</div>

Thanks a lot Mikel… But now i really can’t try whether it’s working  
or not… I think there’s something wrong with my internet connection  
now… after i see you code, i’m wondering, if it’s possible to write  
the code inside the application.js file like this code below :  
"  
function initialize() {  
if (GBrowserIsCompatible()) {  
alert(“kyaaaaaaaa”)

```
    map = new GMap2(document.getElementById("map_canvas"));
    gdir = new GDirections(map,

```

document.getElementById(“directions”));  
GEvent.addListener(gdir, “load”, onGDirectionsLoad);  
GEvent.addListener(gdir, “error”, handleErrors);  
setDirections(\<%= session[:address1] -%\>, \<%= session[:address2] -%\>,  
“en\_US”);  
}  
}  
"

Regards

Adi

---

<div class="post-metadata">

**Author:** ![adi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@adi](https://www.ruby-forum.com/u/adi)\
**Post date:** [April 11, 2008, 9:56am UTC](https://www.ruby-forum.com/t/newbie-in-ror-need-help-about-its-javascript/134473/4 "2008-04-11T09:56:10Z")

</div>

On 11 Apr 2008, at 08:45, Adi wrote:

> ```
> map = new GMap2(document.getElementById("map_canvas"));
> gdir = new GDirections(map,
> 
> ```
> 
> document.getElementById(“directions”));  
> GEvent.addListener(gdir, “load”, onGDirectionsLoad);  
> GEvent.addListener(gdir, “error”, handleErrors);  
> setDirections(\<%= session[:address1] -%\>, \<%= session[:address2] -%\>,  
> “en\_US”);  
> }  
> }  
> "

application.js isn’t passed through erb, so no.  
If I were you I’d make that initialize function take the 2 addresses  
as its arguments and call it from somewhere where you do have access  
to the session.

Fred

---

<div class="post-metadata">

**Author:** ![adi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@adi](https://www.ruby-forum.com/u/adi)\
**Post date:** [April 11, 2008, 10:46am UTC](https://www.ruby-forum.com/t/newbie-in-ror-need-help-about-its-javascript/134473/5 "2008-04-11T10:46:50Z")

</div>

Thanks for the information Fred, i’m just confused from where i can  
access the session…?? I still can’t try the Mikel’s solution, cause  
i think there are troubles with my internet connection…

Regards,

Adi

---

<div class="post-metadata">

**Author:** ![adi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@adi](https://www.ruby-forum.com/u/adi)\
**Post date:** [April 14, 2008, 6:37am UTC](https://www.ruby-forum.com/t/newbie-in-ror-need-help-about-its-javascript/134473/6 "2008-04-14T06:37:56Z")

</div>

On Mon, Apr 14, 2008 at 12:48 PM, Adi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Mikel, I’ve tried what you suggested me to do. And the result is i got  
> an unknown error. After a few trial, i know that if i replace  
> “setDirections(”\<%= session[:address1] -%\>“, “\<%= session[:address2] -  
> %\>”, “en\_US”);” with fixed command such as “setDirections(“new york”,  
> “boston”, “en\_US”);” then the error will never showed up… In my  
> opinion, the setDirections function, somehow did not passing the right  
> parameters…

Where did you put that javascript? In the view template? Or in  
application.js?

Mikel

---

<div class="post-metadata">

**Author:** ![adi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@adi](https://www.ruby-forum.com/u/adi)\
**Post date:** [April 14, 2008, 4:48am UTC](https://www.ruby-forum.com/t/newbie-in-ror-need-help-about-its-javascript/134473/7 "2008-04-14T04:48:40Z")

</div>

Mikel, I’ve tried what you suggested me to do. And the result is i got  
an unknown error. After a few trial, i know that if i replace  
“setDirections(”\<%= session[:address1] -%\>", “\<%= session[:address2] -  
%\>”, “en\_US”);" with fixed command such as “setDirections(“new york”,  
“boston”, “en\_US”);” then the error will never showed up… In my  
opinion, the setDirections function, somehow did not passing the right  
parameters…

If you don’t mind, would you share me any opinion…

Regards,

Adi

---

<div class="post-metadata">

**Author:** ![adi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@adi](https://www.ruby-forum.com/u/adi)\
**Post date:** [April 14, 2008, 11:34am UTC](https://www.ruby-forum.com/t/newbie-in-ror-need-help-about-its-javascript/134473/8 "2008-04-14T11:34:33Z")

</div>

i put only initialize function in the view template and the other  
function i put them in application.js…

Adi

---

<div class="post-metadata">

**Author:** ![adi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@adi](https://www.ruby-forum.com/u/adi)\
**Post date:** [April 15, 2008, 2:10am UTC](https://www.ruby-forum.com/t/newbie-in-ror-need-help-about-its-javascript/134473/9 "2008-04-15T02:10:42Z")

</div>

On Mon, Apr 14, 2008 at 7:33 PM, Adi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i put only initialize function in the view template and the other  
> function i put them in application.js…

Well, the \<%= -%\> parts _have_ to be in your view template. They can  
_not_ be in the application.js.

The \<%= -%\> contains ruby code that will only get executed if it is in  
an ERB template.

Regards

Mikel
