在Rails应用程序中放置Facebook元标记 [英] Placing Facebook Metatags in Rails Application

查看:63
本文介绍了在Rails应用程序中放置Facebook元标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了Omniauth和Koala7 gem,以将我的应用程序与Facebook集成. 一切都运行良好,但在使用自定义对象发布自定义操作方面存在一个小问题.

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.

问题是我的对象url应该是新创建的帖子的显示页面,例如/posts/1.为了使该页面被识别为Facebook对象,我需要将Facebook元标记放在 show.html.erb 之上,如下所示:

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>

问题在于,facebook 对象调试器将其识别为type:webpage而不是.我认为这是因为/layouts/application.html.erb 中已经有默认的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标签在体内:您有的标签ouside.这是因为您的格式不正确,并且它们在分析树中的位置降低了,或者是您不小心将Open Graph标记放在了错误的位置.无论哪种方式,您都需要在代码可用之前对其进行修复.

那我该如何解决这个问题呢? 我需要在我的show.html.erb中放置facebook metatag,但是页面本身已经是整个应用程序布局主体的一部分.

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.

推荐答案

根据您发布的错误消息,我收集到您的元标记不在<head>中,应该位于它们中.使用content_for块是一个很好的情况.在show.html.erb视图中,将所需的元标记放入content_for :head块中.这将保存html,并允许您将其插入布局中的其他位置.

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 %>

然后只需将yeild(:head)添加到您的应用程序模板.您放置在show视图中的html将被插入到应用程序模板中的该位置.我们在此处检查是否为零,以便将产量视为可选的.

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>

这篇关于在Rails应用程序中放置Facebook元标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆