Meta Tags

Episode #26 by Teacher's Avatar David Kimura

Summary

Adding Meta Tags to your website is important for SEO ranking and content display. Learn how to easily add meta tags to your Ruby on Rails application.
rails seo view 4:39

Resources

Summary

# Gemfile
 gem 'meta-tags'

# config/initializers/meta_tags.rb
MetaTags.configure do |c|
  c.title_limit        = 70
  c.description_limit  = 160
  c.keywords_limit     = 255
  c.keywords_separator = ', '
end

# views/layouts/application.html.erb
  <%= display_meta_tags %>
  <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>

# app/controllers/articles_controller.rb
def show
  set_meta_tags title: @article.name,
            site: 'Cat Blog',
            reverse: true,
            description: @article.summary, 
            keywords: Faker::Lorem.words(4),
            twitter: {
              card: "summary",
              site: "@crazycatlady",
              title: "Crazy About Cats",
              description:  @article.summary,
              image: 'http://lorempixel.com/320/240/cats'
            },
            og: {
              title:    "Cat Blog",
              description: @article.summary,
              type:     'website',
              url:      article_url(@article),
              image:    'http://lorempixel.com/320/240/cats'
            },
            alternate: [
              { href: 'http://example.fr/base/url', hreflang: 'fr' },
              { href: 'http://example.com/feed.rss', type: 'application/rss+xml', title: 'RSS' }
            ]
end