7

I have a rails application that helps users submit articles. Does any one know how I can make users in the application share those articles on facebook using their facebook address? A good reference or tutorial would be good.

ipd
  • 5,674
  • 3
  • 34
  • 49
Uchenna
  • 4,059
  • 6
  • 40
  • 73

5 Answers5

12

You can open a popup to http://www.facebook.com/sharer.php?u=URL. This will display a share on facebook page with a link to your image.

For example: http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.google.com

TJHeuvel
  • 12,403
  • 4
  • 37
  • 46
  • Does this mean that i can display a facebook icon in my app and use this link http://www.facebook.com/sharer.php?u=URL as a reference of the icon and it would share that article on facebook? – Uchenna Mar 17 '11 at 14:14
  • Absolutely, this is also the way BBC does it. Check the Facebook image at the bottom of the page: http://news.bbc.co.uk/sport2/hi/rugby_union/english/9425530.stm – TJHeuvel Mar 17 '11 at 14:22
  • please can you try a do it i a ruby on rails way with little example like <%= link_to image_tag('share'), @article %> with the url involved – Uchenna Mar 17 '11 at 14:37
  • I'm sorry, i dont know RoR. But creating a popup shouldnt be all too difficult, you should use Javascript for that anyway. – TJHeuvel Mar 17 '11 at 14:52
  • This seems like a super easy solution, thanks. Are there any disadvantages to doing it this way (as opposed to fb's own like button)? – umezo Jan 09 '13 at 04:07
  • 1
    @umezo I dont think so, since so many sites use this way. For example time.com, they too open a popup to this URL. – TJHeuvel Jan 09 '13 at 09:24
  • Is there a way I can know that sharing on facebook was successful or not. I mean how can my app know that the user actually did shared on facebook rather than just click on share link and cancel afterwards ? – xor Nov 05 '14 at 06:49
4

The "sharer" link has been deprecated. Check out the updated documentation here:

https://developers.facebook.com/docs/reference/dialogs/feed/

A simple example using the Direct URL approach in Rails looks like this:

<%= link_to('Share on Facebook', 'https://www.facebook.com/dialog/feed?
                                  link=https://developers.facebook.com/docs/reference/dialogs/&
                                  picture=http://fbrell.com/f8.jpg&
                                  name=Facebook%20Dialogs&
                                  caption=Reference%20Documentation&
                                  description=Using%20Dialogs%20to%20interact%20with%20users.&
                                  redirect_uri=https://mighty-lowlands-6381.herokuapp.com/',
                                  :target => :blank %>
Tom Rossi
  • 11,604
  • 5
  • 65
  • 96
4

Here is the ruby on rails syntax for it:

<%= link_to image_tag('/images/FaceBookIcon.png'), 'http://www.facebook.com/sharer.php?u='+request.fullpath, :target => :blank %>
McDowell
  • 107,573
  • 31
  • 204
  • 267
1

i found out a trick for sharing to facebook with Oauth token here is my link

parameters were broken with line breaks to make them more readable

<a target="_blank" 
   href="https://www.facebook.com/dialog/feed
     ?app_id=134530986736267
     &link=<%= request.original_url %>
     &name=<%= @article.title%>
     &picture=<%=image_url(@article.image)%>
     &redirect_uri=http://facebook.com/" 
   class="icon icon-facebook">
</a>

Source : http://bechirsegni.me/articles/how-to-share-articles-on-facebook-without-oauth-access-token

Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
Bechir Segni
  • 71
  • 1
  • 8
1

I had to piece together a few answers to arrive at a working solution.

You can include this in the bottom of your Article show page:

<%= link_to "https://www.facebook.com/sharer/sharer.php?u=#{article_url(@article)}",  title: 'Share Me!' do %>
<i class="huge facebook icon" data-content="Share Me!"></i>
<% end %>

It's also worth noting that the Facebook page that opens as result of this button being clicked should probably also be aware of the title of the article, and maybe a snippet of its text, or a description. You can set the title of the page using this: Rails 3 - Ideal way to set title of pages.

Community
  • 1
  • 1
Mark
  • 113
  • 7