Didier Prophete
2006-Jan-14 19:42 UTC
[Rails] link_to_remote where the url contain a javascript variable
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 -- Posted via http://www.ruby-forum.com/.
Didier Prophete
2006-Jan-15 02:54 UTC
[Rails] Re: link_to_remote where the url contain a javascript variab
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 -- Posted via http://www.ruby-forum.com/.
Mark Reginald James
2006-Jan-15 03:22 UTC
[Rails] Re: link_to_remote where the url contain a javascript variable
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.