How to Add Custom OG Images to Your Ruby on Rails App
Ruby on Rails does not include OG tag management out of the box, but the meta-tags gem fills that gap cleanly. Combined with a well-structured ApplicationController and a shared layout partial, you can have consistent social cards across every page with minimal boilerplate. Here is how to wire it up.
meta-tags gem for structured OG metadata
Add `gem "meta-tags"` to your Gemfile and run `bundle install`. In your application layout, replace the default title tag with `<%= display_meta_tags site: "Your Site" %>`. This helper reads values set via `set_meta_tags` in your controllers or views. In any controller action, call `set_meta_tags og: { image: @post.og_image_url, title: @post.title, description: @post.excerpt }` before rendering. The gem outputs correctly structured `og:image`, `og:title`, and `og:description` tags with no additional configuration.
Set default OG image in ApplicationController
In `ApplicationController`, use `before_action` to set default meta tags for every page: `before_action :set_default_meta`. In the private method, call `set_meta_tags og: { image: ActionController::Base.helpers.asset_url("og-default.png") }`. Individual controllers override the default by calling `set_meta_tags` in their specific actions, which merges into the defaults rather than replacing them. Store your default OG image in `app/assets/images/` or reference a CDN URL from your environment config.
Store og_image_url on your models
Add an `og_image_url` string column to any table that represents shareable content. Populate the column when the record is created, either from a callback that calls your image generation service or from a background job triggered by an ActiveRecord callback. In your controller, pass `@post.og_image_url` to `set_meta_tags`. Fall back to the default in ApplicationController if the column is nil. Avoid generating the image URL in the view layer since it complicates caching and mixes concerns.
Turbo Frames and Turbo Drive meta tag behavior
If your Rails app uses Hotwire with Turbo Drive, page navigations update the `<head>` from the response, which means OG tags set by the server are applied correctly on each navigation. However, social crawlers do not execute JavaScript, so they only see the initial server-rendered HTML. Ensure your OG tags are in the full page layout and not inside a Turbo Frame, since Turbo Frame responses return partial HTML without the full `<head>`. Standard controller actions that render full page responses work correctly with any OG metadata strategy.
Generate your OG image in seconds
Paste a title, pick a brand color, and get production-ready social cards for every platform — with framework-specific meta tag snippets included.
Generate an OG image for your Rails app