Rails Meta Tags, undefined method `set_meta_tags' in Rails 4

Gemstone Usage Meta Tags

The documentation states this in the controller:

set_meta_tags :title => 'Member Login',
              :description => 'Member login page.',
              :keywords => 'Site, Login, Members'

      

But, here's what I did, not sure if I need to wrap something around it:

class ApplicationController < ActionController::Base
  rescue_from CanCan::AccessDenied do |exception|
    redirect_to main_app.root_path, :alert => exception.message
  end   
  protect_from_forgery with: :null_session
  before_filter :configure_devise_params, if: :devise_controller?

  set_meta_tags :og => {
                    :title    => 'The Rock',
                    :type     => 'video.movie',
                    :url      => 'http://www.imdb.com/title/tt0117500/',
                    :image    => 'http://ia.media-imdb.com/rock.jpg',
                    :video    => {
                      :director => 'http://www.imdb.com/name/nm0000881/',
                      :writer   => ['http://www.imdb.com/name/nm0918711/', 'http://www.imdb.com/name/nm0177018/']
                    }
                  }
end

      

Then, in my opinion:

<%= display_meta_tags %>

      

But I am getting the error undefined method 'set_meta_tags'

+3


source to share


1 answer


I usually place it in my application controller to keep it tidy.

# app/config/application.rb

# ...

before_action :please_set_meta_tags

def please_set_meta_tags
  set_meta_tags title:       'website name',
                description: 'website description',
                og: {
                  title:     'website name',
                  type:      'website',
                  url:       request.original_url,
                  image:     { _: view_context.image_url('myimage1.jpg'), width: 1200, height: 630 },
                             { _: view_context.image_url('myimage2.jpg'), width: 1200, height: 630 }
                }
end
# ...

      

# app/views/layouts/application.html.erb

...
<head>
  <%= display_meta_tags %>
  ...

      



I use view_context.image_url()

instead image_url()

to get the image as I am not in the view (therefore required view_context

).

And remember, restart your server after installing the gem ! (it can be so obvious that you will forget it)

-1


source







All Articles