2013-03-22 46 views
2

我有一個Rails應用程序,允許用戶鍵入文本。我想知道如何使文本格式化?Rails文本格式

例如,當用戶在文本輸入字段中創建一個新段落時,我希望在呈現要顯示的文本時顯示這些空白,但此時它只是一大塊沒有空格的文本。

有什麼建議嗎?

預先感謝

回答

6

使用simple_format here文檔。 m確定它會工作,

這裏我如何使用它在我的項目;

= simple_format my_text 

其中

my_text = "foo bar 
Testing helloworold foo bar 
Testing helloworld foo bar 
Testing helloworld foo bar 
Testing helloworld foo bar 
Testing helloworld foo bar 
" 

這將讓顯示與輸入你在文中給出,並會呈現所有空格也。

+0

非常有用thx – Batman 2015-08-14 22:32:07

7

當用戶使用在文本輸入(或文本區域)<Enter>字符,它將被正確地顯示了進去。

Rails對這個角色沒有特別的特質。然後,當您渲染它時,您會創建一些HTML,如<%= my_text %>。但<Enter>字符不是以HTML格式呈現的。

你可以使用simple_format來做到這點:它用<br/>代替所有的\n(輸入字符)。

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format

+0

這工作。謝謝隊友 – 2013-03-22 12:05:24

+0

不錯!不要忘記設置一個接受的aswer。 – pierallard 2013-03-22 12:06:25