views:

56

answers:

3

Hi,

In the controller, I have an variable @xml_string = "<tag> hello \n world </tag>". Now I want to show the content of @xml_string. In erb file I wrote <%= @xml_string %>, but this can only display hello world, the xml tag <tag> </tag> was missed and \n was ignored.

Aslo , <% render :text => @xml_string , :content_type = 'application/xml' %> would not show any thing at all.

what is the correct way to achieve this? Thanks.

+1  A: 

try:

<%=h @xml_string %>
khelll
@khell, now I can show `<tag>` but `\n` was still ignored. +1
pierr
change the double quotes to single quotes when assigning the string to the @xml_string var
khelll
Actually, the content was read from xml file using `File.open("a.xml").read`. What should I do in this case? Thanks
pierr
+1  A: 

HTML ignores new line characters and white spaces unless you wrap the content into a tag that is whitespace-aware.

<pre><%=h @xml_string %></pre>

Otherwise, replace the "\n" with a line break. In this case you need to manually escape the HTML string.

<%=h @xml_string.gsub("<", "&lt;").gsub("\n", "<br>") %>
Simone Carletti
+1  A: 

You could use this:

<%=h @xml_string.dump[1..-2] %>

The dump method will simply return the string in a way that makes str == eval(str.dump). That means it includes the quotes, so you need the [1..-2] to slice those away.

dvyjones