Ruby on Rails-社交分享按钮宝石;修改自动填充的内容

时间:2018-11-23 06:10:39

标签: ruby-on-rails ruby

问题:如何使用社交分享按钮gem自动填充我想要的某些社交媒体文本区域?

由于没有太多有关社交分享按钮gem(https://github.com/huacnlee/social-share-button)的文档,我希望在用户单击每个按钮(例如FaceBook和Reddit。

基本上,当我显示一个商品时,我希望用户单击相应的社交媒体按钮来说“我刚从该网站购买了该商品”。

这是我的代码:

<%= social_share_button_tag('I have bought, '+ @item.name + ' , from The Store: ', :allow_sites => %w(twitter)) %>

此代码非常适用于twitter。如它所说:

I have bought, Book, from The Store: http://localhost:3000/items/57#

这很简单,但是尝试为Reddit或Facebook自动填充区域是行不通的。

这是我得到的FB代码:

<%= social_share_button_tag('Share to Facebook', :url => item_path(@item), desc:  @donate_item.name, :allow_sites => %w(facebook)) %>

此代码的作用是将URL和项目标题放在链接中,如下所示:

FB Image

我想在“说点什么”中有一个自动填充的文本,我不确定该怎么办。

关于Reddit共享链接,我已经知道了:

<%= social_share_button_tag(@item.name, :allow_sites => %w(reddit)) %>

它产生:

RedditImage

类似于FB共享,我希望能够自动填写“标题”区域。我已经修改了代码,例如:

<%= social_share_button_tag(@item.name, title: "This item is great!", :allow_sites => %w(reddit)) %>

它不起作用。

任何帮助都会很棒,因为我只是不知道在实际来源中查找什么。

1 个答案:

答案 0 :(得分:1)

检查gem本身,我认为它不支持将标题传递给Reddit。

如果我们查看social_share_button_tag的来源:https://github.com/huacnlee/social-share-button/blob/master/lib/social_share_button/helper.rb,则可以看到它在点击时调用了SocialShareButton.share(this);

然后在https://github.com/huacnlee/social-share-button/blob/6d329c9fadddd159e2b78310f8220f7e56a8d701/app/assets/javascripts/social-share-button.coffee中,我们可以看到它为每个不同的社交媒体平台生成的实际URL,当我们寻找Reddit时,我们看到:

# line 95
when "reddit"
        SocialShareButton.openUrl("http://www.reddit.com/submit?url=#{url}&newwindow=1", 555, 400)

您会注意到存在url参数,但没有标题(与传递标题的其他URL相比。

同时,查看Reddit的API:https://github.com/reddit-archive/reddit/wiki/API:-submit提交对gem的拉取请求以支持标题参数看起来并不费力,并且可能是最好的选择。