使用此代码
<dd><%= textilize @box.description %></dd>
浏览器显示
<p>description text</p>
如何隐藏
标签??
使用此代码
<dd><%= textilize @box.description %></dd>
浏览器显示
<p>description text</p>
如何隐藏
标签??
这取决于您的要求。如果您一般不想要块级标签,则可以使用精简模式:
来自文档:http ://redcloth.rubyforge.org/classes/RedCloth/TextileDoc.html
r = RedCloth.new( "And then? She *fell*!", [:lite_mode] )
r.to_html
#=> "And then? She <strong>fell</strong>!"
否则,我已经为降价做了一些事情,我允许一组非常有限的标签,并且只允许在原始文本中有换行符的情况下使用块元素,如下所示:
def markdown_to_html(markdown_text)
allowed_tags = %w(a em strong)
# Only allow block elements if there are line breaks within the text. This avoids
# unnecessary <p> elements wrapping short single-line texts.
allowed_tags += %w(ul ol li p) if markdown_text.strip.index("\n")
ActionController::Base.helpers.sanitize(Kramdown::Document.new(markdown_text).to_html, :tags => allowed_tags)
end
调整以适应!
另请参阅此旧线程https://www.ruby-forum.com/topic/175551,其中提到了 Rails pre-3 中的方法 textilize_without_paragraph() 已移至现已废弃的 gem。也许那里的代码会对您有所帮助: https ://github.com/dtrasbo/formatize/blob/master/lib/formatize/helper.rb
编辑:啊,我从此后出现在问题上的评论中看到,您正在看到正在呈现的实际标签文本,并且似乎对使用 html_safe 并将段落标签保留在 HTML 中而不是完全摆脱它们感到满意,这是我以为你想要的。那好吧!也许我会把这个答案留给后代。