开发者

How to display arbitrary text in HTML?

In my Rails 3 app, I have a form that asks users to write a note. For example, the text the user inputs might be

@note = "This is a note\n\nwith whitespace."

How do I ma开发者_如何转开发ke this actually render as

This is a note

with whitespace.

when I display the note in my view? (I'm looking for a general solution, not one that simply replaces \n with <br /> or such.)


Go with the <pre> tag as @Moak suggests

Use CSS for line wrapping:

pre {
  white-space: -moz-pre-wrap; /* Mozilla, supported since 1999 */
  white-space: -pre-wrap; /* Opera 4 - 6 */
  white-space: -o-pre-wrap; /* Opera 7 */
  white-space: pre-wrap; /* CSS3 
  word-wrap: break-word; /* IE 5.5+ */
}


In your view, either:

<pre><%= @note %></pre>

Or:

<%= @note.gsub(/\n/, '<br />') %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜