Vincent Bakker

Rendering Markdown with RedCarpet in Rails

Go to app/Gemfile and add

gem 'redcarpet'

now in rails root directory

bundle install

Open app/helpers/application_helper.rb and add the following function:

def markdown(text)
    options = [:hard_wrap, :autolink, :no_intra_emphasis, :fenced_code_blocks]
    Markdown.new(text, *options).to_html.html_safe
end

Edit your view to include

markdown(@changelogs)

And done!

15 november 2021

Hide flash messages using Stimulus

When converting some old code to stimulus I needed a way to hide the flash messages that Rails generates in native javascript.

(Flash messages are notifications to inform a user that a CRUD action has taken place. E.g. User updates successfully.)

import { Controller } from 'stimulus';

export default class extends Controller {
  static targets = ['message']

  connect() {
    this.timeout = setTimeout(this.closeFlash, 2500);
  }

  disconnect() {
    clearTimeout(this.timeout);
  }

  closeFlash = () => {
    this.element.style.display = 'none';
  }

  get flashBox() {
    return $(this.element);
  }
}
29 augustus 2020