A Simpler Way to Set Content Types in Rails

Rails now lets you set content types with less code. This new feature makes your work easier.

The Change #

You can now use a short symbol instead of a long text string. Here’s what it looks like:

Old way:

response.content_type = "text/html"

New way:

response.content_type = :html

Benefits #

This change helps in three ways. First, your code becomes shorter. Second, it matches how Rails handles other formats. Third, you don’t need to remember the full content type text.

Using the Feature #

To try this, update your Rails version. Then start using symbols for your content types.

Credit #

Petrik de Heus built this feature. His work helps make Rails better for everyone.

Reading this because something is going wrong?

A free code audit gives you a written assessment of your codebase in plain English.

Get a Free Code Audit

Rated 4.8/5 on Clutch · you keep the write-up either way

Comments