Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

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

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top