Titles
Introduction
MetaTags sets head metadata in Rails apps: titles, descriptions, canonical URLs, robots directives, and social tags.
Set tags with set_meta_tags in controllers or views, then render them in
the layout with display_meta_tags.
- MetaTags README (github.com)
Page title
set_meta_tags title: 'Member Login'
# <title>Member Login</title>
Set it in a controller or view, then render it with display_meta_tags in the layout.
See: Titles
Site name
set_meta_tags site: 'Site Title', title: 'Member Login'
# <title>Site Title | Member Login</title>
The site name is joined to the page title with a separator.
See: Titles
Separator and order
set_meta_tags(
site: 'Site Title',
title: 'Member Login',
reverse: true,
separator: '·'.html_safe
)
# <title>Member Login · Site Title</title>
Also use prefix, suffix, and lowercase to tune the rendered title.
See: Allowed options
Mirrored values
set_meta_tags og: {
title: :title,
site_name: :site
}
A symbol mirrors another tag: og:title becomes the page title, without the site name.
See: Mirrored values
In views
Display tags
<head>
<%= display_meta_tags site: 'My website' %>
</head>
Render tags in the layout, where display_meta_tags outputs them.
See: MetaTags usage
Set tags individually
<% title 'Member Login' %>
<% description 'Member login page.' %>
<% keywords 'tag1, tag2' %>
These helpers set tags in views. title also returns the title for headings.
Setup
Generate initializer
rails generate meta_tags:install
Creates config/initializers/meta_tags.rb for changing gem defaults.
See: Configuration
Configure defaults
MetaTags.configure do |config|
config.truncate_array_items_at_boundaries = true
end
Set global defaults here once, such as array truncation behavior.
See: Configuration
Metadata
Description
set_meta_tags description: 'This is a sample description'
# <meta name="description" content="This is a sample description">
Search engines may use it for snippets, or use visible page text instead.
See: Description
Keywords
set_meta_tags keywords: %w[keyword1 keyword2 keyword3]
# <meta name="keywords" content="keyword1, keyword2, keyword3">
Legacy tag kept for compatibility; Google and Bing ignore it.
See: Keywords
Canonical URL
set_meta_tags canonical: 'http://yoursite.com/canonical/url'
# <link rel="canonical" href="http://yoursite.com/canonical/url">
Consolidate duplicate URLs under one preferred address.
See: Canonical URL
Icon
set_meta_tags icon: {href: '/favicon.png', type: 'image/png'}
# <link rel="icon" type="image/png" href="/favicon.png">
Pass a path for a default icon, or a hash to set type and sizes.
See: Icon
Alternate links
set_meta_tags alternate: {'fr' => 'http://yoursite.fr/page'}
# <link rel="alternate" href="http://yoursite.fr/page" hreflang="fr">
Map locale codes to URLs for translated or region-specific pages.
See: Alternate links
Pagination
set_meta_tags(
prev: 'http://yoursite.com/url?page=1',
next: 'http://yoursite.com/url?page=3'
)
Legacy prev and next hints; Google no longer uses them as an indexing signal.
See: Pagination links
Robots
Indexing directives
set_meta_tags noindex: true, follow: true
# <meta name="robots" content="noindex, follow">
Combine noindex, nofollow, and follow as needed; follow cancels nofollow.
See: Noindex
Custom directives
set_meta_tags robots: {
'max-snippet' => -1,
'max-video-preview' => -1
}
Use robots: or googlebot: hashes for directives beyond the basic helpers.
See: Robots
Social tags
Open Graph
set_meta_tags og: {
title: 'The Rock',
type: 'video.movie',
image: 'http://ia.media-imdb.com/rock.jpg'
}
Nested hashes map to og:* properties; arrays emit repeated tags.
See: Open Graph
Twitter cards
set_meta_tags twitter: {
card: 'summary',
site: '@username'
}
X cards render from twitter:; Open Graph fields can serve as fallbacks.
See: Twitter cards
0 Comments for this cheatsheet. Write yours!