【问题标题】:Ruby on Rails - Social Share Button Gem; Modifying what is filled automaticallyRuby on Rails - 社交分享按钮宝石;修改自动填充的内容
【发布时间】:2018-11-23 15:45:26
【问题描述】:

问题:如何使用社交分享按钮 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 和 item 的标题放入链接中,如图所示:

我想在“说点什么”中有一个自动填充的文本,但我不知道该怎么做。

关于 Reddit 共享链接,我有这个:

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

它会产生:

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

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

而且它不起作用。

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

【问题讨论】:

标签: ruby-on-rails ruby


【解决方案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 提交拉取请求以支持标题参数看起来工作量不大,可能是您最好的选择。

【讨论】:

  • @AlekseiMatiushkin 哦,我的错,改为评论。我应该删除我的答案吗?
  • 好吧,这取决于你,但它有点违反How To Answer 的准则,因为它确实不包含答案
  • @MichaelFok 你应该这样做。除非您自己深入研究帮助程序并且您将提供代码来完成所要求的操作,否则请坚持发表评论。
  • 谢谢@MarcinKołodziej,我会继续删除它
  • 决定返回并真正深入研究宝石,发现一些我认为应该有助于 OP 的细节? (因为图书馆没有做 OP 希望它做的事情)
猜你喜欢
  • 2017-07-09
  • 1970-01-01
  • 2011-10-30
  • 2017-08-21
  • 2019-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-29
相关资源
最近更新 更多