newbie in ROR need help about its javascript

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");       }     } "

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] -%> <script type="text/javascript">      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");        }      } </script> <% end -%>

And then take it out of application.js

Regards

Mikel

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

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

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

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

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

Mikel

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

Adi

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