Placing Facebook Meta Tags in a Rails Application

I used the Omniauth and Koala7 stones to integrate my app with Facebook. Everything works well except for one minor issue with posting custom actions with custom objects.

The problem is that my object url needs to be the show page of the newly created post like / posts / 1. For this page to be recognized as a facebook object I need to put facebook meta tags on top of 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 type:webpage

instead type:post

. I think it has something to do with the main header tag already in the /layouts/application.html.erb tag:

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

      

I am assuming this because the debugger object specifically states:

Meta tags in body: you have tags owned by yours. This is either because you had the wrong character and dropped them lower in the parse tree, or you accidentally put your Open Graph tags in the wrong place. In any case, you need to fix this before tags are used.

So how do I solve this problem? I need to place facebook meta tags in mine show.html.erb

, but the page itself is already part of the body of the whole app layout.

+3


source to share


1 answer


Based on the error message you posted, I understand that your meta tags are not in the <head>

way they should be. This is a great use case for a block content_for

. In the show.html.erb view, put the desired meta tags in the block content_for :head

. This saves the html and allows it to be inserted 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 yeild(:head)

to your app template. The html you placed in the view will be inserted at that location in the application template. We are checking zero here so that the output 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>

      

+19


source







All Articles