Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

meta-tags

> 后端框架
Open source

Search Engine Optimization (SEO) for Ruby on Rails applications.

2.8K stars0 likes1 views
WebsiteGitHub

About

Search Engine Optimization (SEO) for Ruby on Rails applications.

MetaTags: Rails helpers for SEO metadata, sharing, canonical URLs, and robots

MetaTags helps Ruby on Rails applications render HTML head metadata such as titles, descriptions, canonical links, robots directives, Open Graph tags, X card tags, and hreflang links.

It supports the metadata layer of search engine optimization (SEO), especially for titles, descriptions, canonicalization, robots directives, and social sharing previews.

Ruby on Rails

The MetaTags main branch fully supports Ruby on Rails 6.1+ and is tested against all major Ruby on Rails releases.

[!NOTE] We no longer support Ruby versions older than 3.0 and Ruby on Rails older than 6.1 since they reached their end of life (see Ruby and Ruby on Rails).

[!IMPORTANT] MetaTags manages HTML head metadata. It does not generate structured data / JSON-LD, robots.txt, sitemaps, internal links, or page content.

Installation

Add the "meta-tags" gem to your Gemfile.

ruby
gem "meta-tags"

And run bundle install command.

Configuration

MetaTags ships with practical defaults for truncation and rendering. You can change them to match your application. Legacy fields such as keywords are still supported for compatibility, but they are not modern search requirements.

To override the defaults, create an initializer config/initializers/meta_tags.rb using the following command:

bash
rails generate meta_tags:install

When a truncation limit is reached, arrays passed to title or keywords can either partially truncate the last item or stop at item boundaries. Set config.truncate_array_items_at_boundaries = true to preserve whole items for multi-item arrays. Single-item arrays are still truncated normally.

Symbols in nested custom tag arrays are literal values in MetaTags 2.x. MetaTags 3.0 will use them to look up normalized top-level tags. You can turn on this behavior now. Until then, each nested array that contains a direct Symbol value emits a deprecation warning:

ruby
MetaTags.configure do |config|
  config.resolve_symbolic_references_in_arrays = true
end

When this option is on, use Strings for literal array values. MetaTags does not render a tag when a Symbol has no match.

By default, meta tags are rendered with the key name. However, some meta tags are required to use property instead (like Facebook Open Graph object). The MetaTags gem allows you to configure which tags to render with the property attribute. The pre-configured list includes all possible Facebook Open Graph object types by default, but you can add your own in case you need it.

MetaTags Usage

First, add this code to your main layout:

erb
<head>
  <%= display_meta_tags site: "My website" %>
</head>

Then, to set the page title, add this to each of your views (see below for other options):

erb
<h1><%= title "My page title" %></h1>

When views are rendered, the page title will be included in the right spots:

xml
<head>
  <title>My website | My page title</title>
</head>
<body>
  <h1>My page title</h1>
</body>

You can find allowed options for the display_meta_tags method below.

[!IMPORTANT] You must use display_meta_tags in the layout files to render the meta tags. In the views, you will instead use set_meta_tags, which accepts the same arguments but does not render anything in the place where it is called.

Using MetaTags in controller

You can define the following instance variables:

ruby
@page_title = "Member Login"
@page_description = "Member login page."

Also, you could use the set_meta_tags method to define all meta tags simultaneously:

ruby
set_meta_tags(
  title: "Member Login",
  description: "Member login page."
)

You can find the allowed options for the set_meta_tags method below.

Using MetaTags in view

To set meta tags, you can use the following methods:

erb
<% title "Member Login" %>
<% description "Member login page." %>
<% nofollow %>
<% noindex %>
<% refresh 3 %>

Also, the set_meta_tags method exists:

erb
<%
  set_meta_tags(
    title: "Member Login",
    description: "Member login page."
  )
%>

You can pass an object that implements the #to_meta_tags method and returns a Hash:

ruby
class Document < ApplicationRecord
  def to_meta_tags
    {
      title: title,
      description: summary
    }
  end
end

@document = Document.first
set_meta_tags @document

The title method returns the title itself, so you can use it to show the title somewhere on the page:

erb
<h1><%= title "Member Login" %></h1>

If you want to set the title and display another text, use this:

erb
<h1><%= title "Member Login", "Here you can login to the site:" %></h1>

Allowed options for display_meta_tags and set_meta_tags methods

Use these options to customize the generated tags:

Option Description
:site Site title
:title Page title
:description Page description
:keywords Legacy keywords meta tag for compatibility; ignored by Google Search and Bing web search
:charset Page character set
:prefix Text between site name and separator
:separator Text used to separate the website name from the page title
:suffix Text between separator and page title
:lowercase When true, the page name will be lowercase
:reverse When true, the page and site names will be reversed
:noindex Add noindex meta tag; when true, "robots" will be used; accepts a string with a robot name or an array of strings
:index Add index meta tag; when true, "robots" will be used; accepts a string with a robot name or an array of strings
:nofollow Add nofollow meta tag; when true, "robots" will be used; accepts a string with a robot name or an array of strings
:follow Add follow meta tag; when true, "robots" will be used; accepts a string with a robot name or an array of strings
:noarchive Add noarchive meta tag; when true, "robots" will be used; accepts a string with a robot name or an array of strings
:robots Add custom directives to the robots meta tag (Hash)
:googlebot Add custom directives to the googlebot meta tag (Hash)
:bingbot Add custom directives to the bingbot meta tag (Hash)
:canonical Add canonical link tag
:prev Add legacy prev pagination link tag
:next Add legacy next pagination link tag
:image_src Add legacy image_src share hint
:og Add Open Graph tags (Hash)
:twitter Add Twitter tags (Hash)
:refresh Refresh interval and optionally URL to redirect to

And here are a few examples to give you ideas.

erb
<%= display_meta_tags separator: "&mdash;".html_safe %>
<%= display_meta_tags prefix: false, separator: ":" %>
<%= display_meta_tags lowercase: true %>
<%= display_meta_tags reverse: true, prefix: false %>
<%= display_meta_tags og: { title: "The Rock", type: "video.movie" } %>
<%= display_meta_tags alternate: { "zh-Hant" => "http://example.com.tw/base/url" } %>

Allowed values

You can specify :title as a string or array:

ruby
set_meta_tags title: ["part1", "part2"], site: "site"
# site | part1 | part2
set_meta_tags title: ["part1", "part2"], reverse: true, site: "site"
# part2 | part1 | site

If you still need the legacy keywords tag, values can be passed as a string of comma-separated values or as an array:

ruby
set_meta_tags keywords: ["tag1", "tag2"]
# tag1, tag2

The description is a string (HTML will be stripped from the output string).

Mirrored values

Sometimes, it is desirable to mirror meta tag values down into namespaces. A common use case is when you want the open graph's og:title to be identical to the title.

Let's say you have the following code in your application layout:

ruby
display_meta_tags og: {
  title: :title,
  site_name: :site
}

The value of og[:title] is a symbol, which refers to the value of the top-level title meta tag. In any view with the following code:

ruby
title "my great view"

You will get this open graph meta tag automatically:

xml
<meta property="og:title" content="my great view"></meta>

[!NOTE] The title does not include the site name. If you need to reference the exact value rendered in the <title> meta tag, use :full_title.

Using with Turbo

Turbo is a simple solution for getting the performance benefits of a single-page application without the added complexity of a client-side JavaScript framework. MetaTags supports Turbo out of the box, so no configuration is necessary.

In order to update the page title, you can use the following trick. First, set the ID for the <title> HTML tag using MetaTags configuration in your initializer config/initializers/meta_tags.rb:

ruby
MetaTags.configure do |config|
  config.title_tag_attributes = {id: "page-title"}
end

Now in your turbo frame, you can update the title using a turbo stream:

xml
<turbo-frame ...>
  <turbo-stream action="update" target="page-title">
    <template>My new title</template>
  </turbo-stream>
</turbo-frame>

Using with pjax

jQuery.pjax is a nice solution for navigation without a full-page reload. The main difference is that the layout file will not be rendered, so the page title will not change. To fix this, when using a page fragment, pjax will check the fragment DOM element for a title or data-title attribute and use any value it finds.

MetaTags simplifies this with the display_title method, which returns the fully resolved page title (including site, prefix/suffix, etc.). But in this case, you will have to set default parameters (e.g., :site) both in the layout file and in your views. To minimize code duplication, you can define a helper in application_helper.rb:

ruby
def default_meta_tags
  {
    title: "Member Login",
    description: "Member login page.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubymeta-tagsopengraphopengraph-tagsrails

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架