我有一個textarea輸入,允許用戶輸入超過段落,這意味着用戶可以輸出一個新的行。rails - 用段落輸出輸出用戶輸入?
問題是,當我從數據庫輸出它收縮爲一行時?
如何將新行變成HTML友好的新行,使其看起來像用戶輸入文本的方式?
感謝
我有一個textarea輸入,允許用戶輸入超過段落,這意味着用戶可以輸出一個新的行。rails - 用段落輸出輸出用戶輸入?
問題是,當我從數據庫輸出它收縮爲一行時?
如何將新行變成HTML友好的新行,使其看起來像用戶輸入文本的方式?
感謝
使用simple_format幫手。這是一個從API通過<br />
或<p>
例子替換所有\ N:
my_text = "Here is some basic text...\n...with a line break."
simple_format(my_text)
# => "<p>Here is some basic text...\n<br />...with a line break.</p>"
more_text = "We want to put a paragraph...\n\n...right there."
simple_format(more_text)
# => "<p>We want to put a paragraph...</p>\n\n<p>...right there.</p>"
simple_format("Look ma! A class!", :class => 'description')
# => "<p class='description'>Look ma! A class!</p>"
這也適用。在開關輸入顯示 將這個(變化您類和屬性,TEX:@ post.content或@ post.body)。
<%= @class.attribute.html_safe.gsub(/\r\n?/,"<br/>").html_safe %>
這CSS規則應該做的伎倆:
white-space: pre-line;
感謝這偉大的工作 – AnApprentice 2010-11-20 17:04:32