Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to display an exsiting xml file to user in RoR

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.

like image 715
pierrotlefou Avatar asked Dec 30 '22 09:12

pierrotlefou


2 Answers

have you tried to add

:content_type => 'application/xml'

to your render line?

render :file=>"/path/to/file.xml", :content_type => 'application/xml'
like image 139
jhwist Avatar answered Jan 08 '23 08:01

jhwist


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.

like image 22
Max Chernyak Avatar answered Jan 08 '23 07:01

Max Chernyak