LoginSignup
0
1

More than 1 year has passed since last update.

rails+gem rinkuで改行ありの自動リンク

Posted at

この記事の概要

上記で改行ありで投稿されたテキストのリンク部分を自動でリンク化

gemのinstall

gem 'rinku'

改行について

<%= raw Rinku.auto_link(safe_join(@plan.description.split("\n"),tag(:br)) %>
  • scriptを含んでいたら嫌なのでhでエスケープ
  • 自動エスケープ無しで出力したいのでraw。これがないと自動エスケープされてタグが見える形になる
safe_join(@plan.description.split("\n"),tag(:br)

上記にしているのは単純にsimple_formatだと連続改行が反映されないため

別タブで開くようにする

Rinku.auto_link(text, :all, 'target="_blank"')

上記で別タブで開くようになる。それぞれ下記の意味。

用語 意味
text 変換したい文字列かhtml
mode :all,:urls,::email_adressを指定し、変換したいものを選択
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1