MetaTags

repository·main·Indexed 25 days ago

https://github.com/kpumuk/meta-tags

A Ruby on Rails gem providing helpers to manage HTML head metadata. It supports SEO titles, descriptions, canonical URLs, robots directives, Open Graph, and X (Twitter) card tags. The library includes methods for setting tags in controllers via instance variables or the `set_meta_tags` method, as well as view helpers and a configuration system for truncation and attribute rendering.

Tokens
6.2K
Snippets
26
Records
50
Agent score
78%

What's inside meta-tags

  1. Configure MetaTags for Turbo compatibility

    main

    To allow Turbo to update the page title via Turbo Streams, you must first configure the <title> tag to have a specific ID in a MetaTags initializer.

    1. Create config/initializers/meta_tags.rb:
    MetaTags.configure do |config|
      config.title_tag_attributes = {id: "page-title"}
    end
    1. Use a Turbo Stream in your frame to update the title:
    <turbo-stream action="update" target="page-title">
      <template>My new title</template>
    </turbo-stream>
    # config/initializers/meta_tags.rb
    MetaTags.configure do |config|
      config.title_tag_attributes = {id: "page-title"}
    end
  2. Implement MetaTags in your Rails application

    main

    To use MetaTags, you must first add display_meta_tags to your main layout file (e.g., application.html.erb) within the <head> tag. This method is responsible for actually rendering the tags.

    In your individual views, use the title helper to set the page title. The title helper returns the string itself, allowing you to display it in your HTML (like <h1> tags) while simultaneously setting the <title> tag in the head.

    <head>
      <%= display_meta_tags site: "My website" %>
    </head>
    
    <!-- In your view -->
    <h1><%= title "My page title" %></h1>
  3. Set meta tags in Views

    main

    In your Rails views, you can set meta tags using individual helper methods or the set_meta_tags method.

    Individual helpers:

    • <% title "String" %>
    • <% description "String" %>
    • <% nofollow %>
    • <% noindex %>
    • <% refresh 3 %>

    Using set_meta_tags: Pass a hash of options to set_meta_tags. You can also pass an object that implements #to_meta_tags returning a Hash (e.g., an ActiveRecord model).

  4. Configure MetaTags defaults

    main

    MetaTags provides practical defaults for truncation and rendering. To customize these settings, generate a configuration initializer using the Rails generator:

    rails generate meta_tags:install

    This creates config/initializers/meta_tags.rb where you can override default behaviors.

  5. Mirror meta tag values using symbols

    main

    You can mirror top-level meta tag values into nested namespaces (like Open Graph) by using symbols. For example, using :title inside the :og hash will automatically use the value set by the title helper.

    Note: :title does not include the site name. To reference the exact value rendered in the <title> tag (including site, prefix, etc.), use :full_title.

    <%= display_meta_tags og: {
      title: :title,
      site_name: :site
    } %>
  6. Set meta tags in Controllers

    main

    You can define meta tags in your Rails controllers using two methods:

    1. Instance Variables: Define @page_title and @page_description to be picked up by the helpers.
    2. set_meta_tags method: Call set_meta_tags directly in the controller action to define multiple tags at once using a hash of options. This method does not render anything; it only sets the state for the view.
    # Using instance variables
    @page_title = "Member Login"
    @page_description = "Member login page."
    
    # Using set_meta_tags
    set_meta_tags(
      title: "Member Login",
      description: "Member login page."
    )
  7. Configure array truncation boundaries

    main
    When a truncation limit is reached for title or keywords arrays, you can control whether the truncation happens mid-item or at item boundaries. Set config.truncate_array_items_at_boundaries = true to ensure that multi-item arrays only truncate at the end of a whole item. Single-item arrays are always truncated normally.
  8. Configure meta tag attribute rendering (name vs property)

    main
    By default, meta tags are rendered using the name attribute. However, certain tags (like Facebook Open Graph) require the property attribute. The gem comes pre-configured with a list of Open Graph types that use property. You can extend this list in your initializer to include custom tags that require the property attribute instead of name.
  9. Set meta tags using instance variables in controllers

    main

    You can set SEO metadata in your Rails controllers by defining specific instance variables. The MetaTags library automatically processes these variables during the rendering cycle to populate the meta tags state.

    Supported instance variables:

    • @page_title: Sets the page title.
    • @page_description: Sets the page description.
    • @page_keywords: Sets the legacy keywords tag.
    class MyController < ApplicationController
      def show
        @page_title = 'Member Login'
        @page_description = 'Member login page.'
        @page_keywords = 'Site, Login, Members'
      end
    end
  10. Configure the MetaTags gem

    main

    Use MetaTags.configure to set global configuration settings for the gem. The block yields a Configuration object where you can define settings such as title_limit.

    MetaTags.configure do |config|
      # config.title_limit = 100
    end