首页 > 代码库 > rails 字符串 转化为 html

rails 字符串 转化为 html

simple_format              http://apidock.com/rails/v4.0.2/ActionView/Helpers/TextHelper/simple_format
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html
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>"

simple_format(my_text, {}, wrapper_tag: "div")
# => "<div>Here is some basic text...\n<br />...with a line break.</div>"

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>"

simple_format("<blink>Unblinkable.</blink>")
# => "<p>Unblinkable.</p>"

simple_format("<blink>Blinkable!</blink> It‘s true.", {}, sanitize: false)
# => "<p><blink>Blinkable!</span> It‘s true.</p>"

一个\n被认为是换行其之后是<br/>  两个\n被认为是分段其之前是</p>之后<p>

If you want to escape all content, you should invoke the h method before calling the text helper.
simple_format h(<a href="http://example.com/">Example</a>)
# => "<p>&lt;a href=http://www.mamicode.com/"http://example.com/\"&gt;Example&lt;/a&gt;</p>"

 

rails 字符串 转化为 html