ruby and javascript

A

anja

Hi,

I am working on a little ruby on rails project. I want to call a ruby
method out of the java_script code. The method takes two parameters,
which I have as javascript variables. How can I pass this javascript
variables to the ruby method?

Here is the source code of the rhtml file:

function updateMap(countryCode, city) {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
<% if GeoCoding.hasGeo(countryCode, city) then -%>
map.panTo(<%= GeoCoding.geo(countryCode, city) %>);
<% end -%>
}
}

I get the error :

undefined local variable or method `countryCode' for
#<#<Class:0xb761c578>:0xb761c4c4>

I understand why I get this error. But I have no idea how I can fix it.

Thanks,
anja
 
R

Robert Klemme

anja said:
Hi,

I am working on a little ruby on rails project. I want to call a ruby
method out of the java_script code. The method takes two parameters,
which I have as javascript variables. How can I pass this javascript
variables to the ruby method?

Here is the source code of the rhtml file:

function updateMap(countryCode, city) {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
<% if GeoCoding.hasGeo(countryCode, city) then -%>
map.panTo(<%= GeoCoding.geo(countryCode, city) %>);
<% end -%>
}
}

I get the error :

undefined local variable or method `countryCode' for
#<#<Class:0xb761c578>:0xb761c4c4>

I understand why I get this error. But I have no idea how I can fix it.

Thanks,
anja

You can't call Ruby from JavaScript. JS is client side (i.e. executed
in the browser) while Ruby is server side. The Ruby code is executed
long before the client code; the browser sees only the generated
HTML/JS. If you want to do this check you need to fire off a HTTP
request to your server, have him do the work and process the result in JS.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,536
Members
48,282
Latest member
Xyprime

Latest Threads

Top