Meta Rails
Collection of view helpers to improve search engine optimization of Ruby on Rails application by including appropriate meta tags.
Installation
Open your Rails application's Gemfile
and add this line:
gem 'meta-rails'
Save Gemfile
and execute bundle
command to install the gem.
Title
Based on Google Help and Moz guidelines a proper title consist of page title, brand name, and separator between them.
How to use title
helper
# 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
Meta description tag provides a short summary of the page (refer to this and this articles for details).
How to use meta_description
helper
# 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
Robots meta tag can control the behavior of search engine crawling and indexing (refer to this and this articles for details).
How to use robots
helper
# 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.
License
meta-rails
© Dmitriy Tarasov. Released under the MIT license.