项目作者: jonathantribouharet

项目描述 :
Manage HTML meta tags for SEO in Ruby On Rails
高级语言: Ruby
项目地址: git://github.com/jonathantribouharet/jt-rails-meta.git
创建时间: 2015-06-30T22:18:10Z
项目社区:https://github.com/jonathantribouharet/jt-rails-meta

开源协议:MIT License

下载


JTRailsMeta

Gem Version

JTRailsMeta help you to manage HTML meta tags like title, description, keywords used in Search Engine Optimization (SEO).

Installation

JTRailsMeta is distributed as a gem, which is how it should be used in your app.

Include the gem in your Gemfile:

  1. gem 'jt-rails-meta', '~> 1.0'

Create a meta.yml file for the translations:

  1. rails g jt:meta

Usage

Basic usage

Include JT::Rails::Meta in your ApplicationController:

  1. class ApplicationController < ActionController::Base
  2. include JT::Rails::Meta
  3. ...
  4. end

Call meta_tags in your layout:

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8" />
  4. <%= meta_tags %>
  5. </head>
  6. <body>
  7. </body>

You have also access to meta_title, meta_description, meta_keywords methods.

Define your meta in meta.yml file:

  1. en:
  2. meta:
  3. # in general you use either prefx or suffix for the title of your page
  4. # prefix or suffix are not applied on default title and are both optional
  5. prefix: "My Website - "
  6. suffix: " - My Website"
  7. # default meta used if no meta are found for a page
  8. default:
  9. title: My WebSite
  10. description: My super website is about something magnificent
  11. keywords: "website, some keywords"
  12. # Exemple of meta for the controller users and the action new
  13. # title, full_title, description and keywords are all optional
  14. users:
  15. new:
  16. title: Sign up
  17. description: Description of sign up page
  18. keywords: "sign up, registration"
  19. # Another example for the controller home and the action index
  20. # full_title is used if exceptionally you don't want to use the prefix or suffix
  21. # you can use either title or full_title
  22. home:
  23. index:
  24. full_title: Home
  25. description: Description of homepage

Pass parameters to tags

In your controller:

  1. class PostsController < ApplicationController
  2. def show
  3. @post = Post.find(params[:id])
  4. set_meta_title({ title: @post.title })
  5. set_meta_description({ title: @post.title, author: @post.author })
  6. add_meta_keywords(@post.tags.map(&:name))
  7. end
  8. end

In your meta.yml file:

  1. en:
  2. meta:
  3. posts:
  4. show:
  5. title: "%{title}"
  6. description: "Post about %{title} by %{author}"

Add more tags

You can add more tags with add_meta_extra and add_meta_link methods:

  1. add_meta_extra 'robots' => 'noindex,nofollow'
  2. add_meta_extra {
  3. twitter: {
  4. site: '@mywebsite',
  5. domain: 'mywebsite.com',
  6. title: meta_title,
  7. description: meta_description,
  8. image: [
  9. 'http://mywebsite.com/image_1.jpg',
  10. 'http://mywebsite.com/image_2.jpg'
  11. ]
  12. }
  13. }
  14. add_meta_link 'author', 'https://github.com/jonathantribouharet'
  15. add_meta_link 'publisher', 'https://github.com/jonathantribouharet'

There is some methods already created using add_meta_link method:

  • add_meta_link_canonical which is equivalent to add_meta_link 'canonical'
  • add_meta_link_author which is equivalent to add_meta_link 'author'
  • add_meta_link_publisher which is equivalent to add_meta_link 'publisher'
  • add_meta_link_alternate which is equivalent to add_meta_link 'alternate'

Author

License

JTRailsMeta is released under the MIT license. See the LICENSE file for more info.