Markdown Rendering Before Rails 8.1 vs Rails 8.1

A concise look at how Rails 8.1 makes Markdown a core Rails feature, eliminating custom renderers and simplifying how Markdown content is stored and displayed.

Chaitali Khangar

Technical Architect

A red ruby gem along with text Markdown Rendering in Rails 8.1

Have you ever seen text like this in a Rails application and wondered why it feels so natural to read, yet we never display it like this in our web application?

# Project Name 

A short description of what this project does. 

 ## Features 

- Fast setup 

- Easy configuration 

- Works with Rails 8.1 

We have always seen this in README.md files, GitHub issues, internal documentation, and nowadays in responses generated by AI.

The interesting part is not the format itself, but how Rails has historically treated it.

This is Markdown text.

Markdown is created to solve a very specific problem:

To write structured, readable content without writing HTML.

Why is everyone obsessed with Markdown?

  • It lets you show meaning and structure using plain text

  • You can write headings, lists, links, and code without HTML

  • The text stays easy to read even before it is rendered

  • ·Content, is simple to review and edit

That’s why everyone prefers markdown.

Implementation:

You are building a Rails application that has a Post model. Post contains title and body.

Instead of storing raw HTML in the database, you decide to store the post body as Markdown.

So your Post model ends up looking like this:

class Post < ApplicationRecord 

  # title:string 

  # body:text (stored as Markdown) 

end

In the database, the body column will look like:

## Why Rails 8.1 Matters 

Rails 8.1 introduces native Markdown rendering. 

  ### Benefits 

- Cleaner code 

- Fewer dependencies 

Before Rails 8.1

Step 1: Add a commonmarker renderer gem

gem "commonmarker" 

Step 2: . Convert Markdown to HTML

module MarkdownHelper 

    def markdown_to_html(text) 

          Commonmarker.to_html(text.to_s) 

     end 

end 


Step 3: Render HTML in the view

<%= sanitize(markdown_to_html(@post.body)) %>

You can now see Markdown content converted to HTML and rendered properly in the browser.

After Rails 8.1

Step 1: Register the Markdown MIME type

# config/initializers/mime_types.rb

Mime::Type.register "text/markdown",:md

Step 2: . Add a to_markdown method

class Post < ApplicationRecord 

        def to_markdown = body 

end 


Step 3: Respond with Markdown from the controller


class PostsController < ApplicationController 

  def show 

    @post = Post.find(params[:id]) 

 
    respond_to do |format| 

      format.html 

      format.md { render markdown: @post } 

    end 

  end 

end 

You can now see Markdown content rendered properly in the browser.

Rails 8.1 formalizes Markdown as a native response format, removing the need for custom rendering logic.

Written by Chaitali Khangar

Other blogs

You may also like


Your one-stop shop for expert RoR services

join 250+ companies achieving top-notch RoR development without increasing your workforce.