1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

simple_formatとrails_autolinkを掛け合わせて使うと便利

Last updated at Posted at 2019-07-16

文字列の中からリンクを自動で生成してくれる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'}) %>

こうしてあげると色々便利です。

1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?