# How to Render Markdown Views in Rails

*Published on May 25, 2023*

---


For a personal project, I wanted to render markdown files in my Rails views. Not fetching and rendering markdown from the database, but rendering the plain markdown files under the&nbsp;`views`&nbsp;directory.

At first, I thought it would be complicated, involving searching for views, parsing the markdown content, and rendering it. But a little Google search revealed that Rails already supports this functionality, with a few lines of configuration. This also lets you render them as partials within other views.

Let's see how to accomplish this in two simple steps.

## Step 1: Add a Handler to Convert Markdown into HTML

Let's add a new class called&nbsp;`MarkdownHandler`&nbsp;which converts markdown content to HTML using the&nbsp;[Redcarpet](https://github.com/vmg/redcarpet)&nbsp;gem. We'll store it in a&nbsp;`handlers`&nbsp;directory in the&nbsp;`lib`&nbsp;folder.

```ruby
# lib/handlers/markdown_handler.rb

module Handlers
  class MarkdownHandler
    def call(template, source)
      markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
  
      "#{markdown.render(source).inspect}.html_safe"
    end
  end
end
```

The&nbsp;`call`&nbsp;method receives the template file which represents our views and the source, which is the markdown content. Then we convert it to HTML and return the generated HTML string.

## Step 2: Register the Markdown Handler

Next, create a new initializer called&nbsp;`markdown.rb`&nbsp;under the&nbsp;`config/initializers`&nbsp;directory. An initializer is simply a piece of Ruby code that runs at the start to configure your Rails application or external gems.

To learn more about Rails initializers, check out the following article: [A Brief Introduction to Rails Initializers](/introduction-to-rails-initializers)

All our initializer does is register the markdown handler class we just created to handle any markdown templates, i.e. markdown files with the&nbsp;`.md`&nbsp;extension.

```ruby
# config/initializers/markdown.rb

require 'handlers/markdown_handler'

ActionView::Template.register_template_handler :md, Handlers::MarkdownHandler.new
```

The&nbsp;`register_template_handler`&nbsp;method registers an object that knows how to handle template files with the given extensions. This can be used to implement new template types.

The handler object must respond to&nbsp;`call`, which will be passed the template with its content, and should return the rendered template as a String.

**And that's it. We're done!**

To use this, simply create new markdown files in the&nbsp;`views`&nbsp;directory. Don't forget to use the extension&nbsp;`.html.md`&nbsp;so Rails renders the resulting text as HTML. If you want to separate the static content from your application, just prepend the content directory to the views path, as follows:

```ruby
# app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  append_view_path "content"
end
```

Now you can put the static markdown pages in the&nbsp;`content`&nbsp;directory, outside the&nbsp;`app`&nbsp;directory.

In a future post, we'll inspect how the&nbsp;`register_template_handler`&nbsp;is implemented along with the rest of the view functionality in Rails. Meanwhile, if you have any questions or feedback, didn't understand something, or found a mistake, please leave a comment below or&nbsp;[send me an email](mailto:akshay.khot@hey.com?ref=akshays-blog)&nbsp;as always.

I look forward to hearing from you.

