Question

suppose in your controller, you had something like this:

@out = `git log --name-status -n1 ${@hash}`

(which returns a multi-line output to the terminal)

And you wanted to show it in a view like so:

<%= content_tag(:div, @out.html_safe, class: "well") %>

How do you get that output with preserved whitespace?

Était-ce utile?

La solution

Either use the good old <pre> tag or, in your CSS, apply white-space: pre; or white-space: pre-wrap; to the surrounding element. Personally I think <pre> is preferable, since it's semantic.

P.S. I would avoid using html_safe in this instance, as the unescaped output from your shell command could easily break your markup.

Autres conseils

Typically a <pre> tag is used to markup pre-formatted text:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top