Server-rendered web framework

Rails OG Image Integration

Add automatic MyOG.social images to Rails layouts and views with ERB helpers, request URLs, and content_for overrides.

Prerequisites

  • A Rails app with public pages rendered through layouts
  • Access to app/views/layouts/application.html.erb or a shared head partial
  • A MyOG.social account with your domain added

How MyOG fits Rails

Rails layouts render shared head markup around controller views, which makes them the natural place for default Open Graph tags.

Use request.original_url or a canonical URL value from the controller, encode it, and pass it to MyOG so each public route gets its own generated image.

For pages that need custom handling, content_for lets an individual view provide page-specific head tags while the layout keeps a fallback.

1

Choose a layout

MyOG detects page content and branding automatically. The layout only controls image composition.

2

Add the Rails code

ERB layout snippet

Use this in app/views/layouts/application.html.erb or a shared head partial.

<% page_url = request.original_url %>
<% myog_image_url = "https://api.myog.social/og?url=#{ERB::Util.url_encode(page_url)}&template=screenshot-right" %>
<meta property="og:image" content="<%= myog_image_url %>" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content="<%= myog_image_url %>" />

If a controller sets a canonical URL, use that value instead of request.original_url.

3

Place it correctly

  1. Find the layout or partial that renders title, description, and existing Open Graph tags.
  2. Add a MyOG image URL built from request.original_url or your canonical URL helper.
  3. Use content_for for pages that already provide custom social metadata.
  4. Deploy, then inspect the HTML source for a normal page and a dynamic record page.

Avoid duplicate og:image tags

Rails apps often have SEO helpers, meta-tags gems, or layout partials that already emit og:image. Replace that image source with MyOG or gate the fallback so only one preferred og:image appears.

Dynamic pages

For posts, products, docs, and marketing pages, request.original_url works when the route URL is already canonical. If you canonicalize slugs or strip query parameters, build and pass that canonical URL instead.

Test the result

  • View source on a deployed page and confirm the MyOG image URL is in the head.
  • Test one dynamic record route, not just the home page.
  • Confirm the URL sent to MyOG uses the production host, not a staging or localhost host.

Start with the MyOG Open Graph Checker, then refresh social platform caches if needed.

Known limitations

Authenticated pages and draft previews cannot be fetched by MyOG or social crawlers.
Apps behind reverse proxies need correct host/protocol settings or request URLs may be wrong.
If Turbo or client-side JavaScript changes metadata after load, crawlers may still read the original layout HTML.

Sources

Ready to get started?

Sign up for free and add generated social images to Rails pages.

Already have an account?

cdf733b534ea2f2ed964d150330b323c44837e4f