0
1

More than 3 years have passed since last update.

railsでレコードに保存している「連続した改行」を改行を保持したままviewに表示する

Last updated at Posted at 2020-01-15

環境

Rails 5.2.3
Ruby 2.6.3

実現したいこと

例えばArticleモデルのcontentカラムに保存している、
「あいうえお

かきこけこ」
をviewに表示したい。

解決策

safe_join関数を利用し、改行文字をbrに変換する。
simple_format 関数もあるのだが連続改行が認識されないため、
safe_join関数を利用する。

show.html.haml
= safe_join(@article.content.split("\n"), tag(:br))

参照

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