-1

I have a room model and I want to create a share button for facebook. So I decided to do this with the sharer url method. I want to have a facebook icon on the show.html.erb (room view) where a user or visitor can share the room on facebook. When I click on the link I am redirected to facebook where I have to enter the link manually and not automatically which I want to share. If I am updating the show.html.erb link_to into:

<%= link_to image_tag('facebook_icon_red.png'), 'http://www.facebook.com/sharer.php?u=<%= yield :url %>', :target => :blank %>

Then I am getting an error:

enter image description here

How can I make this work?

application.html.erb

<!DOCTYPE html>
<html>
  <head>
...
...
    <!-- for Facebook -->          
    <meta property="og:title" content="<%= @room.listing_name %>" />
    <meta property="og:type" content="room" />
    <meta property="og:image" content="" />
    <meta property="og:url" content="<%= room_url(@room) %>" />
    <meta property="og:description" content="<%= @room.detailed_description %>" />
  </head>

<body>
    <!-- Facebook Sharing Settings-->
    <div id="fb-root">
    </div>
    <script src="http://connect.facebook.net/en_US/all.js" type="text/javascript"></script>
    <script type="text/javascript">
    FB.init({
        appId: MY_ID(SOME NUMBERS),
        status: true,
        cookie: true,
        xfbml: true
    });
    FB.Canvas.setAutoResize();
    </script>
...
...
...
  </body>
</html>

show.html.erb (room)

...
<!-- Do you like this Accommodation? -->
<%= link_to image_tag('facebook_icon_red.png'), "http://www.facebook.com/sharer.php?u=", :target => :blank %>

FB.ui(
   {
     method: 'feed',
     name: 'Facebook Dialogs',
     link: 'http://developers.facebook.com/docs/reference/dialogs/',
     picture: 'http://fbrell.com/f8.jpg',
     caption: 'Reference Documentation',
     description: 'Dialogs provide a simple, consistent interface for applications to interface with users.',
     message: 'Facebook Dialogs are easy!'
   },
   function(response) {
     if (response && response.post_id) {
       alert('Post was published.');
     } else {
       alert('Post was not published.');
     }
   }
);
trickydiddy
  • 577
  • 6
  • 26
  • What for are you calling FB.ui with the feed method? – CBroe Nov 01 '16 at 12:48
  • The facebook javascript API should allow me to share and give me a callback. [source](http://stackoverflow.com/questions/4980061/twitter-and-facebook-share-with-callbacks/6253936#6253936) – trickydiddy Nov 01 '16 at 13:24
  • Yeah, but it has no connection to your link whatsoever, and that link alone triggers sharing here. Your FB.ui call has nothing to do with that link, and happens automatically without the user clicking anything. That makes little sense. – CBroe Nov 01 '16 at 14:42

1 Answers1

0

Your link_to put like this way:

<%= link_to image_tag('facebook_icon_red.png'), "http://www.facebook.com/sharer.php?u=#{yield :url }", :target => :blank %>

This should be work for you.

Kaushlendra Tomar
  • 1,410
  • 10
  • 16
  • I still have to enter a URL manually :( But thanks for trying to help me! – trickydiddy Nov 01 '16 at 12:40
  • 1
    Use `double quotes` when you need to interpolate – Deepak Mahakale Nov 01 '16 at 12:52
  • Thanks allot! Okay if I am using double quotes with the # I still have to type in the URL manually. If I am using double quotes without the # something strange is happening when I am clicking on the link. A new tab is opening and closing immediately?! Any hints? – trickydiddy Nov 01 '16 at 13:21