Collection of view helpers to improve search engine optimization of Ruby on Rails application by including appropriate meta tags.
Open your Rails application's Gemfile
and add this line:
gem 'meta-rails'
Save Gemfile
and execute bundle
command to install the gem.
Based on Google Help and Moz guidelines a proper title consist of page title, brand name, and separator between them.
# Include 'title' helper in the head section of your site
# e.g., ~/app/views/layouts/application.html.erb
<head>
<%= title %>
</head>
# Utilize 'provide' or 'content for' to store page's title
# e.g., ~/app/views/users/new.html.erb
<% provide(:title, 'Sign up') %>
This code will create HTML <title>
element:
<title>Sign up | AwesomeRailsApp</title>
Format of HTML <title>
element can be modified by passing keyword arguments to title
helper:
Argument | Description | Default |
---|---|---|
:identifier |
Identifier for stored page's title | :title |
:brand |
Brand name | Rails.application.class.module_parent_name |
:separator |
Character between title and brand | | |
:reverse |
Switch position of title and brand | false |
Meta description tag provides a short summary of the page (refer to this and this articles for details).
# Include 'meta_description' helper in the head section of your site
# e.g., ~/app/views/layouts/application.html.erb
<head>
<%= meta_description %>
</head>
# Utilize 'provide' or 'content for' to store page's meta description
# e.g., ~/app/views/users/new.html.erb
<% provide(:meta_description, 'Register to manage your account') %>
This code will create HTML <meta>
tag:
<meta name="description" content="Register to manage your account" />
Keyword arguments can be passed to meta_description
helper:
Argument | Description | Default |
---|---|---|
:identifier |
Identifier for stored page's meta description | :meta_description |
Robots meta tag can control the behavior of search engine crawling and indexing (refer to this and this articles for details).
# Include 'robots' helper in the head section of your site
# e.g., ~/app/views/layouts/application.html.erb
<head>
<%= robots %>
</head>
# Utilize 'provide' or 'content for' to store page's robots directives
# e.g., ~/app/views/users/new.html.erb
<% provide(:robots, 'noindex,nofollow') %>
This code will create HTML <meta>
tag:
<meta name="robots" content="noindex,nofollow" />
Keyword arguments can be passed to robots
helper:
Argument | Description | Default |
---|---|---|
:identifier |
Identifier for stored page's robots directives | :robots |
Please refer to valid indexing & serving directives page for list of valid robots directives.
meta-rails
© Dmitriy Tarasov. Released under the MIT license.