Skip to content

Instantly share code, notes, and snippets.

@herko
Created February 1, 2021 12:39
Show Gist options
  • Save herko/662f141b701b9b391514eb8f3a1a305e to your computer and use it in GitHub Desktop.
Save herko/662f141b701b9b391514eb8f3a1a305e to your computer and use it in GitHub Desktop.
Managing SEO tags in rails application
<%# app/views/layouts/shared/_seo.html.erb %>
<%
title = construct_title(yield(:title))
description = yield(:description)
og_image = yield(:og_image)
url = yield(:url).presence || request.original_url
%>
<title><%= title %></title>
<meta property="og:title" content="<%= title %>">
<% if description.present? %>
<meta name="description" content="<%= description %>">
<meta property="og:description" content="<%= description %>" />
<% end %>
<% if og_image.present? %>
<meta property="og:image" content="<%= og_image %>">
<% end %>
<meta property="og:site_name" content="<%= site_name %>">
<meta property="og:url" content="<%= url %>">
<meta property="og:type" content="website">
<meta property="fb:app_id" content="XYZ">
<%# any_page.html.erb %>
<%
seo_title t('seo.pages.home.title')
seo_description t('seo.pages.home.description')
og_image 'main.jpg'
%>
...
# app/helpers/seo_helper.rb
module SeoHelper
def seo_title(title)
content_for(:title, title)
end
def seo_description(description)
content_for(:description, description)
end
def og_image(image)
content_for(:og_image, image_url("og/#{image}"))
end
def construct_title(title)
[title, site_name].select(&:present?).join(title_separator)
end
def site_name
I18n.t("seo.website.title")
end
def title_separator
I18n.t("seo.website.separator")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment