Forum: Ruby on Rails link_to_remote where the url contain a javascript variable

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.
B389820ea2e2ee4f166930dea947a303?d=identicon&s=25 Didier Prophete (didier-prophete)
on 2006-01-14 20:41
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:
    <a href="#" onclick="new Ajax.Updater('my_div',
'/xyz/my_action?var=my_var', {asynchronous:true, evalScripts:true});
return false;">my_link</a>

But what I really want is this:
    <a href="#" onclick="new Ajax.Updater('my_div',
'/xyz/my_action?var=' + my_var, {asynchronous:true, evalScripts:true});
return false;">my_link</a>

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
B389820ea2e2ee4f166930dea947a303?d=identicon&s=25 Didier Prophete (didier-prophete)
on 2006-01-15 03:54
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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-01-15 04:25
(Received via mailing list)
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.
B389820ea2e2ee4f166930dea947a303?d=identicon&s=25 Didier Prophete (didier-prophete)
on 2006-01-15 08:08
Mark,

u the man !!
thanks a bunch bro.

-Didier
This topic is locked and can not be replied to.