# Link\_to\_remote where the url contain a javascript variable

**URL:** <https://www.ruby-forum.com/t/link-to-remote-where-the-url-contain-a-javascript-variable/61446>\
**Category:** Rails\
**Created:** [January 14, 2006, 8:41pm UTC](https://www.ruby-forum.com/t/link-to-remote-where-the-url-contain-a-javascript-variable/61446 "2006-01-14T20:41:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![didier-prophete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@didier-prophete](https://www.ruby-forum.com/u/didier-prophete)\
**Post date:** [January 14, 2006, 8:41pm UTC](https://www.ruby-forum.com/t/link-to-remote-where-the-url-contain-a-javascript-variable/61446/1 "2006-01-14T20:41:58Z")

</div>

Ok, so I am basically trying to use ‘link\_to\_remote’ and pass the value  
of a javascript variable as an argument. Something like that:

link\_to\_remote “my\_link”,  
:update =\> ‘my\_div’,  
:url =\> { :action =\> “my\_action”, :var =\>  
‘my\_var’ }

Basically, ‘my\_var’ is a javascript variable which I want to pass to  
‘my\_action’.

This, of course generates:  
my\_link

But what I really want is this:  
my\_link

Notice the difference in the url:  
‘/slides/my\_action?var=my\_var’  
vs:  
‘/slides/my\_action?var=’ + my\_var  
(I the second case, my\_var is a javascript variable which gets appended  
to the url)

I tried everything I could think of (escaping, etc…), but nothing  
worked. Looking at the code, ‘link\_to\_remote’ call ‘remote\_function’,  
which eventually has this line:  
279: function \<\< “’#{url\_for(options[:url])}’”

And the function ‘url\_for’ is going to try to url-encode whatever I pass  
to it… so whatever I try to escape gets ‘url-encoded’ and there is  
nothing I can do…

Can anybody think of a solution for this ? (I can always write the  
javascript on my own, but it is not as cool as using some rails features  
🙂

-Didier

---

<div class="post-metadata">

**Author:** ![didier-prophete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@didier-prophete](https://www.ruby-forum.com/u/didier-prophete)\
**Post date:** [January 15, 2006, 3:54am UTC](https://www.ruby-forum.com/t/link-to-remote-where-the-url-contain-a-javascript-variable/61446/2 "2006-01-15T03:54:53Z")

</div>

Ok, so after thinking for some time, I came up with this SUPER UGLY  
solution:

\<%= form\_remote\_tag :update =\> ‘my\_div’,  
:before =\> “$(‘hidden\_tag’).value = my\_var”,  
:url =\> { :action =\> “my\_action” } %\>  
\<%= hidden\_field\_tag ‘my\_var’, 0, :id =\> ‘hidden\_tag’ %\>  
\<%= submit\_tag ‘Go’ %\>  
\<%= end\_form\_tag %\>

The idea is to submit a form with a hidden field which gets updated  
right before you submit (with the value of ‘my\_var’), but this is so  
ugly… I am ashamed of myself 🙂

Somebody’s got to have a better solution for this…

-Didier

---

<div class="post-metadata">

**Author:** ![didier-prophete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@didier-prophete](https://www.ruby-forum.com/u/didier-prophete)\
**Post date:** [January 15, 2006, 4:25am UTC](https://www.ruby-forum.com/t/link-to-remote-where-the-url-contain-a-javascript-variable/61446/3 "2006-01-15T04:25:48Z")

</div>

Didier Prophete wrote:

> Ok, so I am basically trying to use ‘link\_to\_remote’ and pass the value  
> of a javascript variable as an argument. Something like that:
> 
> link\_to\_remote “my\_link”,  
> :update =\> ‘my\_div’,  
> :url =\> { :action =\> “my\_action”, :var =\>  
> ‘my\_var’ }
> 
> Basically, ‘my\_var’ is a javascript variable which I want to pass to  
> ‘my\_action’.

Add a

```
   :with => 'my_var'

```

option to your link\_to\_remote call and retrieve it in your  
controller using request.raw\_post

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![didier-prophete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@didier-prophete](https://www.ruby-forum.com/u/didier-prophete)\
**Post date:** [January 15, 2006, 8:08am UTC](https://www.ruby-forum.com/t/link-to-remote-where-the-url-contain-a-javascript-variable/61446/4 "2006-01-15T08:08:09Z")

</div>

Mark,

u the man !!  
thanks a bunch bro.

-Didier
