简体   繁体   中英

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

Using the gem Meta Tags

The documentation states to put this in the controller:

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

But, thats what I did, not sure if I need to have something wrapped 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 view:

<%= display_meta_tags %>

But I'm getting the error undefined method 'set_meta_tags'

I usually place it in the application controller to keep the view 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 of image_url() to get the image as I'm not in a view (therefore view_context is needed).

And remember to restart the server once you installed the gem ! (it might be so obvious that you forget it)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM