2012-01-03 38 views

回答

10

不幸的是 - 你不能。如果您在http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format處查看源代碼,則會看到p標籤無條件地纏繞在內容上。

你可以創建一個使用simple_format代碼幫手,但其修改爲不包括p標籤...

+1

是那裏simple_format替代?所有我希望我的方法顯示爲' *'並且HTML解釋爲 – leonel 2012-01-03 21:11:04

+1

再次 - simple_format只有9行,如果您刪除了第二行,第八行和第九行,不會再放置P標籤了。 但是,我不確定你的意思是「HTML解釋」? – elijah 2012-01-03 21:26:03

+2

在Rails 4中看起來像你可以像'simple_format(my_text,{},wrapper_tag:「div」)那樣改變包裝器標籤,但它在Rails 3中並不適合我 – FireDragon 2013-08-09 08:34:46

1

也許不是你真正想要的,但是......我落得這樣做的:

module ApplicationHelper 
    def nl2br s 
    split_paragraphs(sanitize(s, tags: [])).join('<br>').html_safe 
    end 
end 

UPD或者更好的是:

def nl2br s 
    sanitize(s, tags: []).gsub(/\n/, '<br>').html_safe 
end 
4

您可以指定wrapper_tag選項。

simple_format 'Hello', {}, wrapper_tag: 'span' 

該代碼將是:

<span>Hello</span>