Topic: preventing url_for from escaping strings

I am trying to do something like this


url_for (:controller => 'foo', :action => 'bar', :id => js)

I want to pass some javascript to url_for, but then all the quotes and such get escaped.
would there be any solution to prevent that?

Re: preventing url_for from escaping strings

url_for just generates a URL so it can't really hold javascript. You can add javascript to a link_to method though. Something like this:

link_to 'some link', { :controller => 'foo', :action => 'bar' }, :onclick => js

if you want the "id" of the url to evaluate to the content of a javascript variable or something, then I would construct the url all in javascript and just append the variable to the url string.

Last edited by ryanb (2006-09-06 01:45:42)

Railscasts - Free Ruby on Rails Screencasts

Re: preventing url_for from escaping strings

its not what I want. the url_for output is return to javascript code (remote_function) not link_to thus it can contain javascript.
it whole thing works, except that I can not use url_for, thus I have to build the url by myself instead of using the routing table.

If I could prevent this escaping thing with some options, that be great.

Re: preventing url_for from escaping strings

I found that I can do.

url_for(:action => 'boo', :escape => false) which would prevent the escaping from processing
but it does not work.

I checked the rails code, apparently, only the url_for from the view escape the URL, the controller's one does not take this :escape option. but escape it anyway.

not sure if something is broken, but I seems to be doing it properly.

Re: preventing url_for from escaping strings

so its broken, the url_for from the controller is not suppose to escape the url while it does. and the :escape => false just prevent the url from being escaped AGAIN.

BUG.

Re: preventing url_for from escaping strings

Here's an ugly hack:

url_for(:action => 'foo', :id => '_js_').sub('_js_', js)

Last edited by ryanb (2006-09-06 21:40:21)

Railscasts - Free Ruby on Rails Screencasts

Re: preventing url_for from escaping strings

I like it though. cuz right now I just build the url by string concat and If I change my routes, it won't work.

thanks

by the way, are you Ryan from Vancouver?

Re: preventing url_for from escaping strings

Nope, I'm Ryan from a little town in Oregon.

Railscasts - Free Ruby on Rails Screencasts