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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E08c1146baf37df2665e59e9d712f238?d=identicon&s=25 Rich Johns (richiebaby)
on 2006-01-22 00:10
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.
1c46e952a61c99d9eae4f21037a088fd?d=identicon&s=25 Anatol Pomozov (Guest)
on 2006-01-22 12:34
(Received via mailing list)
Try this


<% remote_function(:update => :name_of_div,
       :url => { :controller => 'nameOfController',
       :action => :nameOfAction },
       :with => "'InputVariable=' + #{$localVariable}") %>
E08c1146baf37df2665e59e9d712f238?d=identicon&s=25 Rich Johns (richiebaby)
on 2006-01-22 15:41
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)) %>
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-22 15: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}\'"

or

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

_Kevin
This topic is locked and can not be replied to.