views:

171

answers:

3

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;amp;amp;yoo=hoo"/&gt;
</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&amp;yoo=hoo" in the source for XML-validity rules, but certainly it shouldn't be double-escaped, should it?

A: 

No it shouldn't, otherwise the generated XML file would be invalid, a correct parser will translate &amp; back to & when parsing the file.

Edit : nevermind, &amp; got translated into & in the first message.

mickael9
James A. Rosen
A: 

I guess it's because XHTML requires the & to be escaped as &amp; (even in URLs) and the XML where you stores the URL requires this too, giving a double escapement (&amp;amp;) which will decode to &amp; 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&amp;c=d:

<a href="http://example.com/?a=b&amp;amp;c=d"&gt;link&lt;/a&gt;

So my guess would be that foos_url returns you an url already containing a &amp;, then it is escaped again by your XML module.

Pierre Bourdon
+1  A: 

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.

Topper