LoginSignup
2
2

More than 5 years have passed since last update.

railsでmeta-tagsを使ってSEOフレンドリーなサイトを目指す

Last updated at Posted at 2019-05-01

概要

SEO目的でmetatagsを使う。下記が目的。

  • SEO的にはmetatag関連(特にtitle)がユニークであることが重要なのでページごとの管理をしやすくしたい
  • SEOに関連するタグを管理、更新しやすくしたい

meta-tagsの導入

gemfile
gem 'meta-tags'
bundle install

helperにdefaultを設定

app/helpers/application_helper.rb
module ApplicationHelper
  def default_meta_tags
    {
      title: "title",
      description: "description",
      keywords: "",
      noindex: ! Rails.env.production?,
      canonical: request.original_url,
      charset: "UTF-8",
      og: {
        title: :title, 
        description: "description",
        type: "website",
        url: request.original_url,
        image: "",
        site_name: "",
        locale: "ja_JP"
      },
      twitter: {
        site: '@ツイッターのアカウント名',
        card: 'summary',
        image: ""
    },
      fb: {
        app_id: ''
      }
    }
  end
end

viewへの設定

app/views/layouts/application.html.rb
<%= display_meta_tags(default_meta_tags) %>

設定ファイルの作成

bundle exec rails generate meta_tags:install

上記コマンドで設定ファイルを作成し、rails sで読込し直す。

各ページで出し分けをしたい場合

view
<% set_meta_tags title: "#{@post.title}" %>
2
2
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
2
2