在 Rails 中 - 是否有将换行符转换为 <br> 的 rails 方法? [英] In Rails - is there a rails method to convert newlines to <br>?

查看:42
本文介绍了在 Rails 中 - 是否有将换行符转换为 <br> 的 rails 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有 Railsy 方法将 \n 转换为 <br>?

Is there a Railsy way to convert \n to <br>?

目前,我是这样做的:

mystring.gsub(/\n/, '<br>')

推荐答案

是的,rails 有 simple_format 可以完全满足您的需求,而且稍微好一点,因为它还添加了段落标签.见

Yes, rails has simple_format which does exactly what you are looking for, and slightly better since it also adds paragraph tags. See

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

示例:

 simple_format(mystring)

请注意,simple_format 允许基本的 HTML 标签,但也可以通过删除所有脚本的 sanitize 传递文本,因此用户输入应该是安全的.

Note that simple_format allows basic HTML tags, but also passes text through sanitize which removes all scripts, so it should be safe for user input.

这篇关于在 Rails 中 - 是否有将换行符转换为 &lt;br&gt; 的 rails 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆