# Passing ruby variable via remote\_function (JavaScript)

**URL:** <https://www.ruby-forum.com/t/passing-ruby-variable-via-remote-function-javascript/63867>\
**Category:** Rails\
**Created:** [January 22, 2006, 12:10am UTC](https://www.ruby-forum.com/t/passing-ruby-variable-via-remote-function-javascript/63867 "2006-01-22T00:10:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![richiebaby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@richiebaby](https://www.ruby-forum.com/u/richiebaby)\
**Post date:** [January 22, 2006, 12:10am UTC](https://www.ruby-forum.com/t/passing-ruby-variable-via-remote-function-javascript/63867/1 "2006-01-22T00:10:45Z")

</div>

Hi,

Brand new to this so please excuse anything obvious that I don’t yet  
get.

Here’s what I’m trying to do (This is all in a .rhtml file):

As a simple test I create a local variable called ‘localVariable’ and  
give it the string “Hello” like this;

\<% $localVariable = ‘Hello’ %\>

Next I want to call a function (nameOfAction) which is expecting to  
receive as input a variable (InputVariable). Now, this all works if I  
hard code in a string in place of $localVariable, but I don’t want to do  
that. The variable is being sent as the name of the variable and not its  
contents. Do I need to escape something???

\<% remote\_function(:update =\> :name\_of\_div,  
:url =\> { :controller =\> ‘nameOfController’,  
:action =\> :nameOfAction },  
:with =\> “‘InputVariable=’ + $localVariable”) %\>

My question is, how do I get the $localVariable to convert to its  
content within the above?

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![richiebaby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@richiebaby](https://www.ruby-forum.com/u/richiebaby)\
**Post date:** [January 22, 2006, 12:34pm UTC](https://www.ruby-forum.com/t/passing-ruby-variable-via-remote-function-javascript/63867/2 "2006-01-22T12:34:09Z")

</div>

Try this

\<% remote\_function(:update =\> :name\_of\_div,  
:url =\> { :controller =\> ‘nameOfController’,  
:action =\> :nameOfAction },  
:with =\> “‘InputVariable=’ + #{$localVariable}”) %\>

---

<div class="post-metadata">

**Author:** ![richiebaby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@richiebaby](https://www.ruby-forum.com/u/richiebaby)\
**Post date:** [January 22, 2006, 3:41pm UTC](https://www.ruby-forum.com/t/passing-ruby-variable-via-remote-function-javascript/63867/3 "2006-01-22T15:41:40Z")

</div>

Thank you Anatol for your suggestion, but in this case it still did not  
work.

After getting some sleep I have managed to fix my problem, I did it like  
this;

\<% remote\_function(:update =\> :name\_of\_div,  
:url =\> { :controller =\> ‘nameOfController’,  
:action =\> :nameOfAction },  
:with =\> “'InputVariable=” + $localVariable + “’”) %\>

It’s all about the arrangement of double and single quotes.

A couple of comments,

a) I am aware that adding $ in front of a variable name makes it global  
and not local as stated.

b) If any beginners (like me) are wondering how I got the  
remote\_function to work within a .rhtml file, the solution is to wrap it  
in a javascript tag;

\<%= javascript\_tag(remote\_function(:upda…and so on)) %\>

---

<div class="post-metadata">

**Author:** ![richiebaby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@richiebaby](https://www.ruby-forum.com/u/richiebaby)\
**Post date:** [January 22, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/passing-ruby-variable-via-remote-function-javascript/63867/4 "2006-01-22T15:56:11Z")

</div>

Rich Johns wrote:

> \<% remote\_function(:update =\> :name\_of\_div,  
> :url =\> { :controller =\> ‘nameOfController’,  
> :action =\> :nameOfAction },  
> :with =\> “'InputVariable=” + $localVariable + “’”) %\>

This should also work…

:with =\> “‘InputVariable = #{$localVariable}’”

or

:with =\> %Q{ ‘InputVariable = #{$localVariable}’ }

\_Kevin
