Placing Facebook Metatags in Rails Application

2019-04-11 20:52发布

I have implemented Omniauth and Koala7 gems to integrate my application with Facebook. Everything works well, except one minor problem with posting custom actions with custom objects.

The problem is that my object url should be the show page of a newly created post, such as /posts/1. To make this page recognized as a facebook object, I need to put facebook metatags on top of the show.html.erb like this:

<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# sdff: http://ogp.me/ns/fb/sdff#">
  <meta property="fb:app_id" content="myid" /> 
  <meta property="og:type"   content="sdff:post" /> 
  <meta property="og:url"    content="<%= "http://sdff.herokuapp.com" + post_path(@post) %>" /> 
  <meta property="og:title"  content="Sample" /> 
  <meta property="og:image"  content="https://fbstatic-a.akamaihd.net/images/devsite/attachment_blank.png" /> 
</head>

The problem is that facebook object debugger recognizes this as a type:webpage instead of type:post. I think this is because there's already the default head tag in /layouts/application.html.erb, like so:

<head>
  <title>sdff</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>

I assume this because the object debugger specifically points out:

Meta Tags In Body: You have tags ouside of your . This is either because your was malformed and they fell lower in the parse tree, or you accidentally put your Open Graph tags in the wrong place. Either way you need to fix it before the tags are usable.

So how do I solve this problem? I need to place the facebook metatags in my show.html.erb, but the page itself is already a part of the body of the entire application layout.

1条回答
Lonely孤独者°
2楼-- · 2019-04-11 21:32

Based on the error message you posted, I gather that your meta tags are not in the <head> as they should be. This is a great case to use a content_for block. In your show.html.erb view, place your desired meta tags into a content_for :head block. This saves the html and allows you to insert it somewhere else in the layout.

<% content_for :head do %>
  <meta property="fb:app_id" content="myid" /> 
  <meta property="og:type"   content="sdff:post" /> 
  <meta property="og:url"    content="<%= "http://sdff.herokuapp.com" + post_path(@post) %>" /> 
  <meta property="og:title"  content="Sample" /> 
  <meta property="og:image"  content="https://fbstatic-a.akamaihd.net/images/devsite/attachment_blank.png" /> 
<% end %>

Then just add a yeild(:head) to your application template. The html you placed in the show view will be inserted into this spot in the application template. We check for nil here so that the yield is considered optional.

<head>
  <title>sdff</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
  <%= content_for?(:head) ? yield(:head) : '' %>
</head>
查看更多
登录 后发表回答