How to show string in xml format in erb file
In the controller, I have an variable @xml_string = "<tag> hello \n world &开发者_开发技巧lt;/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.
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("<", "<").gsub("\n", "<br>") %>
try:
<%=h @xml_string %>
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.
精彩评论