Formatting Text

For text fields one typically wants to allow some HTML tags, add support for link breaks and even auto link URL’s and emails.

This method which I got from this forum thread, Adding a line break … safely, does all of this and is a must add to my application_helper.rb.

def format_content(content)
  # allow only tags specified in tags options, likewise in attributes
  content = sanitize(content, :tags => %w(b strong i em img), :attributes => %w(src))

  # add support for line breaks
  content = simple_format(content)

  # auto link URL's and emails
  content = auto_link(content, :all, :target => '_blank')

  return content
end

Leave a Reply

Your email address will not be published. Required fields are marked *