文字列の中からリンクを自動で生成してくれるgemのrails_autolinkと改行で表示してくれるsimple_formatを掛け合わせてみた。
rails_autolinkをインストール
Gemfile.
gem 'rails_autolink'
これで bundle installを実行する。
掛け合わせて使う
show.html.erb
<%= auto_link(simple_format(@user.description), html: {target: '_blank'}) %>
helperとしてmethod作成
method名はお好みでネーミングすれば良いのですが、
application_helper.rb
def regular_format(arg)
auto_link(simple_format(arg), html: {target: '_blank'})
end
regular_formatという名前で定義してあげれば、先ほどのshowは
show.html.erb
<%= regular_format(@user.description), html: {target: '_blank'}) %>
こうしてあげると色々便利です。