Posted on Leave a comment

How to display text in paragraphs in Rails

A really quick post with some code allowing displaying text in paragraphs in Rails.

I am currently building a simple app for learning languages and yesterday I created for it a blog section with posts. There I want to write article about learning languages and each new line display as separate paragraph.

The following code does this trick. First you split the text into the array and then iterate over it and put each sentence into paragraph. Here’s the code:

  <% p = @post.content.split("\n") %>
  <% p.each do |sentence| %>
  <p><%= sentence %></p>
  <% end %>

I know that this post is relevant for not many people, but I was looking for some time for solution myself, so I hope that this information will be valuable to someone.

Leave a Reply

Your email address will not be published.