How do I prevent a Builder template from escaping a URL in an attribute value?
-
18-09-2019 - |
Question
I have a Rails Builder template:
# in app/views/foos/index.xml.builder:
xml.Module do |mod|
...
mod.Content :type => 'url',
:href => foos_url(:bar => 'baz',
:yoo => 'hoo')
end
(It creates the XML for an OpenSocial Module file, but that's not important.)
The problem is that the rendered XML looks like this:
<Module>
...
<Content type="url" href="http://myapp.com/foos?bar=baz&amp;yoo=hoo"/>
</Module>
That URL suffix should be "bar=baz&yoo=hoo
." How do I keep Builder from escaping the amerpsand?
Later
Perhaps the URL suffix should be "bar=baz&yoo=hoo
" in the source for XML-validity rules, but certainly it shouldn't be double-escaped, should it?
Solution
I know I'm late here - but for anyone coming in from google (like me) the trick is to add an :escape => false to the url_for - that way the url only gets escaped once.
OTHER TIPS
I guess it's because XHTML requires the &
to be escaped as &
(even in URLs) and the XML where you stores the URL requires this too, giving a double escapement (&amp;
) which will decode to &
in the URL, which is perfectly valid in HTML (and mandatory in XHTML).
For example, the following code is valid XHTML linking to http://example.com/?a=b&c=d
:
<a href="http://example.com/?a=b&c=d">link</a>
So my guess would be that foos_url
returns you an url already containing a &
, then it is escaped again by your XML module.
No it shouldn't, otherwise the generated XML file would be invalid, a correct parser will translate & back to & when parsing the file.
Edit : nevermind, & got translated into & in the first message.