[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ruby and javascript

anja

5/17/2006 7:45:00 AM

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

1 Answer

Robert Klemme

5/17/2006 8:14:00 AM

0

anja wrote:
> 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