Ruby-on-rails 在 Rails 中 - 是否有将换行符转换为 <br> 的 rails 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/611609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Rails - is there a rails method to convert newlines to <br>?
提问by daustin777
Is there a Railsy way to convert \n to <br>?
是否有 Railsy 方法将 \n 转换为<br>?
Currently, I'm doing it like this:
目前,我是这样做的:
mystring.gsub(/\n/, '<br>')
回答by Daniel Von Fange
Yes, rails has simple_formatwhich does exactly what you are looking for, and slightly better since it also adds paragraph tags. See
是的,railssimple_format可以完全满足您的需求,并且稍微好一点,因为它还添加了段落标签。看
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format
Example:
例子:
simple_format(mystring)
Note that simple_formatallows basic HTML tags, but also passes text through sanitizewhich removes all scripts, so it should be safe for user input.
请注意,它simple_format允许使用基本的 HTML 标签,但也允许通过文本sanitize删除所有脚本,因此用户输入应该是安全的。
回答by Tomalak
You may make it more general by doing:
您可以通过执行以下操作使其更通用:
mystring.gsub(/(?:\n\r?|\r\n?)/, '<br>')
This way you would cover DOS, *NIX, Mac and accidental invalid line endings.
这样你就可以覆盖 DOS、*NIX、Mac 和意外无效的行尾。
回答by jomo
You should be careful with this when you are dealing with user input.simple_formatinserts <br>tags but it will allow other html tags!
当您处理用户输入时,您应该小心这一点。simple_format插入<br>标签,但它会允许其他 html 标签!
When using simple_format, <b>Hello</b>will be rendered as "Hello", you might not want this.
使用 simple_format 时,<b>Hello</b>将呈现为“ Hello”,您可能不希望这样。
Instead you can use <%= h(c.text).gsub("\n", "<br>").html_safe %>h()will encode the html first, gsubreplaces the line break and html_safeallows the <br>tags to be displayed.
相反,您可以使用<%= h(c.text).gsub("\n", "<br>").html_safe %>h()will 先对 html 进行编码,gsub替换换行符并html_safe允许<br>显示标签。
This will display exactly what the user entered. It also allows to discuss html in e.g. comments.
这将准确显示用户输入的内容。它还允许在例如评论中讨论 html。
回答by bershika
Simply use
只需使用
white-space: pre-line;
in your css and text will wrap on line breaks.
在您的 css 中,文本将在换行符处换行。
回答by Jim Puls
You also might consider what you're trying to do - if you're nicely formatting text that people have entered, you might consider a filter like Markdownto let your users format their text without opening up the can of worms that is HTML. You know, like it is here at Stack Overflow.
您还可以考虑您要尝试做什么 - 如果您要很好地格式化人们输入的文本,您可以考虑使用像Markdown这样的过滤器,让您的用户在不打开 HTML 蠕虫罐的情况下格式化他们的文本。你知道,就像在 Stack Overflow 上一样。
回答by ryeguy
Nope. What you have there is the commonly used alternative. The definition most people use is:
不。你有什么是常用的替代品。大多数人使用的定义是:
def nl2br text
text.gsub(/\n/, '<br/>')
end
It is named as such because it mimics the functionality of the PHP function by the same name.
回答by ryeguy
mystring.gsub(/\r\n|\r|\n/, '\n')
worked for me
对我来说有效
回答by Henrik N
You can do simple_format(h(text))– the hwill ensure any HTML is not rendered.
您可以这样做simple_format(h(text))——这h将确保不会呈现任何 HTML。
As mentioned in other answers, this will do a bit more than you asked for. It wraps the whole thing in <p>, and adds more paragraphs if you have double newlines anywhere.
正如其他答案中提到的,这会比你要求的多一点。它将整个内容包装在 中<p>,如果您在任何地方都有双换行符,则会添加更多段落。

