1. ホーム
  2. ruby-on-rails

[解決済み] In Rails - is there a rails method to convert newlines to <br>?

2022-09-19 15:09:30

Question

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

Currently, I'm doing it like this:

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

How to solved?

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

Example:

 simple_format(mystring)

なお simple_format は基本的な HTML タグを許可していますが、テキストを sanitize を通過させ、すべてのスクリプトを削除します。