Be mindful of which variables require plain text in views and which need html

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the web-development category.

Last Updated: 2024-04-19

When I checked out the episodes#show page in this website (SemicolonAndSons) I saw the text Oxbridge %> poking out of the HTML looking all ugly. The cause was the following

def short_description
  # description contained HTML content stored in the DB (technically: markdown converted into HTML)
  description.truncate
end

which fed into a erb template for a twitter meta tag:

<meta name="twitter:description" content="<%= @episode.short_description %>">

Because the twitter tag only can take plain text, it freaked out when given HTML, thus the overspill of %> symbols into the viewable HTML.

I should have rendered to plain text (instead of HTML) in short_description.

Lessons

In general, I should be aware of the difference in types between:

and keep them separate in my view templates. I guess a sensible distinction would be that model methods/presenter methods return plain text (unless the method name communicates otherwise: _html) and general helper methods can return HTML by default.