Maileditor Docs

Ruby on Rails

A controller action that signs the current user and an ERB view that renders the editor iframe.

Rails renders on the server, so a controller action can sign the user and hand the finished URL to the view. The secret is read from the environment.

Before you start

Follow the first three steps of the quickstart: create a plugin, add localhost:3000 to its allowed domains, and copy the Plugin ID and Secret.

Configuration

Set these in the environment Rails runs in. With dotenv-rails they can live in .env; on a host they go in the app's environment settings.

.env
MAILEDITOR_PLUGIN_ID=9f3c1e7a-4b21-4c8e-9a77-1d2f5b6c8e10
MAILEDITOR_PLUGIN_SECRET=your_plugin_secret
MAILEDITOR_EDITOR_BASE_URL=https://maileditor.net
MAILEDITOR_PLUGIN_VERSION=1.0.0

Sign on the server

config/routes.rb
get "/editor", to: "editor#show"
app/controllers/editor_controller.rb
require "openssl"

class EditorController < ApplicationController
  def show
    user_id = "user-#{current_user.id}"
    expires = Time.now.to_i + 300
    payload = "v1|#{user_id.bytesize}:#{user_id}|#{expires}"
    signature = OpenSSL::HMAC.hexdigest("SHA256", ENV.fetch("MAILEDITOR_PLUGIN_SECRET"), payload)

    query = {
      external_user_id: user_id,
      identity_expires: expires,
      identity_signature: signature,
      plugin_version: ENV.fetch("MAILEDITOR_PLUGIN_VERSION", "1.0.0")
    }.to_query

    @src = "#{ENV.fetch("MAILEDITOR_EDITOR_BASE_URL", "https://maileditor.net")}/plugin-editor/v1/#{ENV.fetch("MAILEDITOR_PLUGIN_ID")}?#{query}"
  end
end

current_user comes from your authentication setup. With Devise, add before_action :authenticate_user! so the action only runs for signed-in users. bytesize counts bytes, which is what the payload requires.

Render the iframe

app/views/editor/show.html.erb
<iframe
  src="<%= @src %>"
  title="Email editor"
  allow="clipboard-write; fullscreen"
  style="width: 100%; height: 100vh; border: 0"
></iframe>

ERB escapes & to &amp; inside the attribute, which the browser decodes back before loading the URL.

Next steps

  • Add template_id to the query to open one of the user's saved templates. See Embedding the editor.
  • If the iframe shows "Plugin editor unavailable", read result.error in the session response. See Error codes.

On this page