rails 3 gem 用于一键在社交媒体网站上共享内容 [英] rails 3 gem for sharing content on social media sites in one click

查看:23
本文介绍了rails 3 gem 用于一键在社交媒体网站上共享内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以一键在不同的社交媒体网站(fb、twitter、google+、soundcloud)上分享我的网络应用程序中的内容?Instagram 也有类似的功能.

Is it possible to share content from my web application on different social media sites (fb, twitter, google+, soundcloud) in one click? Instagram has similar feature.

有可用的 rails gem 吗?教程的指针将不胜感激.

Is there rails gem available for that? Pointers to tutorials will be appreciated.

推荐答案

为什么要使用 gem

虽然实现社交分享按钮的代码确实非常简单,并且通常可以直接从该站点的示例中复制到您的模板中,但使用 gem 来管理该代码是有好处的:

Why use a gem

While it's true the code to implement social sharing buttons is quite straightforward and can usually be copied directly from that site's examples into your template, there are benefits to using a gem to manage that code:

  • 它有助于保持代码库中的关注点分离 (DRY).如果您打算在多个视图中使用这些按钮,您可能无论如何都需要编写一些辅助模板函数.

  • It helps maintain separation of concerns in your codebase (DRY). If you are going to be using these buttons in multiple views, you'd probably want to write some helper template functions anyway.

您不必让您的应用保持最新,因为每个共享按钮的实现有任何变化(前提是您选择的 gem 维护良好)

You don't have to keep your app up to date with any changes in the implementation of each sharing button (provided the gem you choose is well-maintained)

我知道这是一个旧线程,但我偶然发现它正在寻找 Rails 的社交共享宝石",并且因为我找到了一个可行的解决方案,所以我想分享.

I know this is an old thread, but I stumbled upon it looking for "the" social sharing gem for rails, and since I got to a working solution I wanted to share.

我找到了两个维护得比较活跃的 gem:

I was able to find two somewhat actively maintained gems:

  • https://github.com/huacnlee/social-share-button -这支持大量站点,但使用简单的实现.它不支持更高级的功能,如计数器和其他格式,因为它没有调用完整的 iframe 实现.但它似乎对主要的亚洲社交网络有很好的覆盖.
  • https://github.com/iffyuva/social-buttons - 仅支持 Facebook、G+、Twitter 和 Pinterest.但是,它是一个功能更全面的抽象,因此适用于我的用例(FB、G+、Twitter 和计数器).
  • https://github.com/huacnlee/social-share-button - This supports a large number of sites, but uses a simple implementations. It doesn't support more advanced functionality like counters and other formats, because it's not calling the full iframe implementations. But it seems to have excellent coverage of the main asian social networks.
  • https://github.com/iffyuva/social-buttons - This only supports Facebook, G+, Twitter, and Pinterest. However, it is a more fully-featured abstraction and so it worked for my use case (the FB, G+, Twitter, with counters).

要使用后者,一般可以按照github页面上的说明进行操作,但有一些例外:

To use the latter, you can generally follow the instructions on the github page, with a few exceptions:

我不得不将 gem 的源设置为 github 而不是 ruby​​gems,因为 ruby​​gem 行已经过时并且有一些阻塞错误.所以:

I had to set the source of the gem to github instead of rubygems, because the rubygem line was out of date and had some blocking bugs. So:

gem 'social-buttons',git:'git://github.com/iffyuva/social-buttons.git',分支:'master'

如果他们的爬虫无法访问他们的 url,facebook 对话框会在您打开它后立即关闭 - 如果您从 localhost 工作,就会发生这种情况,因此请确保您指定了 URL.确实,您的应用程序应该知道您的内容的规范网址是什么,因此在您看来:

The facebook dialog box keeps closing right after you open it if their crawler can't get to their url - this will happen if you're working from localhost, so make sure you specify the URL. Really, your application should know what your cannonical url for your content is, so in your view:

url = request.protocol + HOST_FOR_CRAWLERS + model_path(@model)

最后,文档对如何为 facebook 函数形成参数有点不清楚,或者您需要 script: true 为 google plus 标记部署其脚本.

Finally, the documentation was a little unclear on how to form arguments for the facebook function, or that you need script: true for the google plus tag to deploy its script.

这是我的观点:

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