Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering line breaks from database text in html

I'm building a cms for video management that includes transcript fields. The transcript fields get sent to the database and almost always have multiple newline ("\n") characters.

I'm running rails 4.2 with bootstrap.

Probably not relevant, but the code:

# Controller
class VideosController < ApplicationController
  def show
    @video = Video.find(params[:id])
  end
end

# View (in haml)
.panel.panel-default
  .panel-body
    %p= @video.transcript

Is there a clean way to render the line breaks properly in html?

like image 523
oolong Avatar asked Aug 24 '15 18:08

oolong


1 Answers

Check the simple_format helper:

Returns text transformed into HTML using simple formatting rules. Two or more consecutive newlines(\n\n) are considered as a paragraph and wrapped in <p> tags. One newline (\n) is considered as a linebreak and a <br /> tag is appended. This method does not remove the newlines from the text.

.panel.panel-default
  .panel-body
    %p= simple_format(@video.transcript)
like image 184
Philidor Avatar answered Oct 08 '22 05:10

Philidor