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.
have you tried to add
:content_type => 'application/xml'
to your render line?
render :file=>"/path/to/file.xml", :content_type => 'application/xml'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With