社交分享帖子

时间:2017-01-23 04:18:09

标签: ruby-on-rails facebook twitter social-networking

我正在尝试添加社交分享链接到我的rails帖子,没有宝石。这是我目前的代码:

 <!-- Facebook -->
 <a onclick="javascript:window.open('http://www.facebook.com/sharer/sharer.php?u=<%= url_for([@post, {only_path: false}]) %>', '_blank', 'width=800, height=500, top=200, left=300');void(0);"><i class="fa fa-facebook" aria-hidden="true"></i></a>

<!-- Twitter -->

<a onclick="javascript:window.open('https://twitter.com/intent/tweet?text=<%= @post.title %> by @someuser - &amp;url=<%= url_for([@post, {only_path: false}]) %>', '_blank', 'width=800, height=500, top=200, left=300');void(0);"><i class="fa fa-twitter" aria-hidden="true"></i></i></a>

示例帖子:https://mirrorcommunications.herokuapp.com/blog/how-to-not-get-booed-at-your-next-presentation

示例来源:view-source:https://mirrorcommunications.herokuapp.com/blog/how-to-not-get-booed-at-your-next-presentation

Facebook尝试打开,然后崩溃和Twitter给我一个页面未找到错误。任何帮助,将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:1)

您的url_for功能正在输出无法为Facebook / Twitter工作的网址。社交网络不知道如何处理/blog/how-to-not-get-booed-at-your-next-presentation - 他们需要方案和域名,即https://mirrorcommunications.herokuapp.com/blog/how-to-not-get-booed-at-your-next-presentation

答案 1 :(得分:1)

终于明白了:

帖子控制器:

# Social Sharing for Single Posts
def original_url
   base_url + original_fullpath
end

查看:

<!-- Facebook -->
<a onclick="javascript:window.open('http://www.facebook.com/sharer/sharer.php?u=<%= request.original_url %>', '_blank', 'width=800, height=500, top=200, left=300');void(0);"><i class="fa fa-facebook" aria-hidden="true"></i></a>

<!-- Twitter -->

<a onclick="javascript:window.open('https://twitter.com/intent/tweet?text=<%= @post.title %> by @someuser - &amp;url=<%= request.original_url %>', '_blank', 'width=800, height=500, top=200, left=300');void(0);"><i class="fa fa-twitter" aria-hidden="true"></i></i></a>

以下是文档:http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-original_url