문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top