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

【Rails】text_areaで改行・段落の表示方法

Posted at

form_withで作成したtext_areaのフォームに改行や段落を入れたい場合があると思いますので、自分への記録も兼ねてこちらでその方法をシェアします。

new.html.erb
<%= form_with(model: @recipe, local: true) do |f| %>
  <div class="col-md-8">
    <%= f.text_area :catchcopy, class: "form-control" %>
  </div>
<% end %>

上記のようにフォームを作成し、それを表示しようとすると、以下のようになります。

show.html.erb
<div>
  <%= @recipe.catchcopy %>
</div>

ただ、このようにすると、入力フォームで改行や段落を加えた場合でも、それが反映されません。

以下のようにコードを書けば、改行・段落が反映されます。

show.html.erb
<div>
  <%= safe_join(@recipe.catchcopy.split("\n"),tag(:br)) %>
</div>

参考になりましたら、幸いです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?