2
0

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 3 years have passed since last update.

入力したtextの改行がviewで表示されない

Posted at

概要

text_areaで入力した文章に改行が入っていたのに、いざ投稿一覧でみてみると改行が反映されていない時の解決法について共有しておきます。

例えば、postのmessageに


xxx
yyy
zzz



入力したつもりが、投稿一覧で確認してみると


xxx yyy zzz


となってしまった時の対処法です。

結論

index.html.erb
<%= post.message %>

となっていた部分を

index.html.erb
<%= simple_format(post.message) %>

としましょう。

simple_formatとは?

simple_formatは、改行文字を含むテキストをブラウザ上で表示させる時に使われるヘルパーになります。simple_formatの機能について簡単にまとめます。

文字列を<p>で囲む
改行には<br/>を付与
連続した改行については</p><p>を付与

(他記事を参照)

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?