Placing Facebook Metatags in Rails Application

天涯浪子 提交于 2019-12-21 19:48:15

问题


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:


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>


来源:https://stackoverflow.com/questions/14929653/placing-facebook-metatags-in-rails-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!