Forum: Ruby on Rails Passing ruby variable via remote_function (JavaScript)

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Rich J. (Guest)
on 2006-01-22 01:10

Brand new to this so please excuse anything obvious that I don't yet

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.
Anatol P. (Guest)
on 2006-01-22 13:34
(Received via mailing list)
Try this

<% remote_function(:update => :name_of_div,
       :url => { :controller => 'nameOfController',
       :action => :nameOfAction },
       :with => "'InputVariable=' + #{$localVariable}") %>
Rich J. (Guest)
on 2006-01-22 16:41
Thank you Anatol for your suggestion, but in this case it still did not

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

<% 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)) %>
Kevin O. (Guest)
on 2006-01-22 16:56
Rich Johns wrote:
> <% remote_function(:update => :name_of_div,
>        :url => { :controller => 'nameOfController',
>        :action => :nameOfAction },
>        :with => "'InputVariable=" + $localVariable + "'") %>

This should also work...

:with => "\'InputVariable = #{$localVariable}\'"


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

This topic is locked and can not be replied to.