Kumaro Kumaro - 5 months ago 7
Ruby Question

Rails String in View with whitespaces

I'm trying to show a string with xml content (correct with whitespaces etc.) in a rails view.

My Controller action read a xml file and put this content into a string.
For example

@xml_content = "
<main>
<children>
<a>
</a>
<b>
</b>
</children>
</main>
"


Then I get this string from my controller and want to show this xml content in a view.

<%= @xml_content %>


But when I print this string in the view all whitespaces get lost and I get something like this:

<main><children><a></a><b></b></children></main>


Printing the same string in the console with "puts @xml_content" create the correct output.

What can I do to show this xml content with all whitespaces in the view?

EDIT:
Controller:

@file = File.open(@xml_device_description.get_file_path)

@xml_content = Nokogiri::XML @file

Answer

To conserve whitespaces of your content, you can wrap it in a pre tag.

<pre>
 <%= @xml_content %>
</pre>