views:

52

answers:

3

Hi,

I have an xml file and I want to display(render) it as it is to the user, i.e I want to keep the tags. How should I do in RoR?

I have tried render :file=>"/path/to/file.xml", but the tag <product> disappeared.

//file.xml
<product>car</product>

Update: I found the behavior is browers-dependent

  • Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008101315 Ubuntu/8.10 (intrepid) Firefox/3.0.3

    The tags are kept.

  • Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.15) Gecko/2009101601 Firefox/3.0.15

    The tags disappeared.

+1  A: 

Hi,
have you tried to add

:content_type => 'application/xml'

to your render line?

render :file=>"/path/to/file.xml", :content_type => 'application/xml'
jhwist
Hi, this still did not work on my Windows version Mozilla.
pierr
Wow, further research shows that it is indeed the browser that is in the way here. IE8 (for what it's worth) displays the xml as expected. As far as my google search showed, this seems to be some sort of religious war within mozilla as to what the Right Thing[tm] would be. Seems you are lost here, sorry.
jhwist
Further investigation shows that whenever an XML file remotely looks like html (by name of tags, in my test using http://www.w3schools.com/xmL/note.xml there is a "body" tag) the Windows Firefox (or maybe just Gecko/2009101601, which is what I have as well) renders only the text, not the elements.
jhwist
A: 

Add an xml prolog line to the file (in addition to the content type suggestion from jhwist).

<?xml version="1.0"?>
MattMcKnight
A: 

Different browsers display XML differently. Some try to be smart, others don't. You can't rely on that. If you want to display XML "as is" you need to render escaped XML as text.

In your controller action you'll have to call this:

render :text => @template.h(File.read("/path/to/file.xml"))

This will escape all HTML for you and act as plain text.

hakunin