0

I am trying to save a long multi-paragraph text description in my database in a text attribute. The problem is that when I render this text, all the formatting is missing and it appears as one long string.

Is there any way to keep the line breaks?

1
  • 1
    There are multiple ways to do it - you can allow html to be processed as your input or you can implement markdown processing of the text field. I'm kind of verbose but this article on my blog is actually the second (with a link to the first) about how I implemented markdown in my blog. I suggest kramdown if you can since it's easier to apply classes Commented Oct 5, 2016 at 23:38

1 Answer 1

3

You can use the simple_format

It will adds formatting rules to the text.

In case if you want to use the formatted description in many places. You can add a method to the model.

For example if you have a model Post with field description

Then you can add a method in the model so that when ever if you use this method formatting will be applied.

  class ContentItem < ActiveRecord::Base

    def formatted_description
      return simple_format(self.description)
    end
  end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.