Link_to_remote where the url contain a javascript variable


#1

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
:slight_smile:

-Didier


#2

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 :slight_smile:

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

-Didier


#3

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.


#4

Mark,

u the man !!
thanks a bunch bro.

-Didier