14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

meta-tagsを使ってSEO周りの設定をすっきり書く!

Posted at

RailsでSEOの設定を行うGemでmeta-tagsというGemがあります。今回はこのGemを使って全ページ共通のSEOの設定や個別ページでの設定をしていきたいと思います。

インストール

Gemfileに以下を追記

gem 'meta-tags'

bundle installでgemを入れる。

設定

まずSEOまわりの設定としてtitleやkeywordなどページごとに異なる設定と
全ページで共通の設定の2つがあるかと思います。

まず全ページで共通の設定をしていきます。
helperに以下を追記

application_helper.rb
def default_meta_tags
  {
    title:       "Title",
    description: "description",
    keywords:    "Ruby,Meta,Tags",
    icon: image_url("favicon.ico"), # favicon
    noindex: ! Rails.env.production?, # production環境以外はnoindex  
    charset: "UTF-8",
    # OGPの設定
    og: {
      title: "title",
      type: "website",
      url: request.original_url,
      image: image_url("OGPで設定する画像"),
      site_name: "site name",
      description: "description",
      locale: "ja_JP"
    } 
  }
end

※ 設定は自身の環境にあわせて変えてください

layoutに以下を追記

application.html.erb
<head>
  <%= display_meta_tags(default_meta_tags) %>
</head>

これでlayoutが適用されている全ページにデフォルトの設定が適用されます。

次にページ毎の設定をviewに追加していきます。

login.html.erb
<%= set_meta_tags title: 'ログインページ' %>

この2つの設定で全体の設定、個別ページの設定ができます。

間違っている点や、上手くいかない点などあればご指摘ください。

14
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?