1

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 

1 Answer 1

3

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

<pre>
 <%= @xml_content %>
</pre>
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a possibility to format the string or to use a kind of syntax highlighting?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.