Imageomatic: badges for quality Rails apps

Metadata

You will probably want to set the title and description of your opengraph metadata. There’s two ways you can do that:

Views

If you think about opengraph metadata as a view concern, you can set it right from your view templates like this:

<% # ./app/views/posts/show.html.erb
  opengraph.title = @post.title
  opengraph.description = @post.summary
%>

<h1><%= @post.title %></h1>
<article><%= @post.body %></article>

This will set the values needed for the opengraph_meta_tags helper that you inserted into your application layout file earlier.

Controller

If you think of the opengraph metadata more as a controller concern, we’ve got you covered:

class PostsController < ApplicationController
  before_action: :assign_opengraph_data

  def edit
    # This overrides the title that's set from `assign_opengraph_data`
    opengraph.title = "Editing #{@post.title}"
  end

  protected

  def assign_opengraph_data
    opengraph.title = @post.title
    opengraph.description = @post.summary
    # Displays the first image of a blog post, instead of a screenshot, for the opengraph image.
    # Assumes the image was managed via ActiveStorage.
    opengraph.image = url_for(@post.images.first)
  end
end