0

I want to share campaign name along with image and description but it only share name

<%= social_share_button_tag(@campaign.project.project_name, :image => @campaign.campaign_image_url) %>

Is there any way to share image on social media?

Haseeb Ahmad
  • 7,914
  • 12
  • 55
  • 133

1 Answers1

2

From the code snippets in the question I'm guessing you're using the social-share-button gem.

Unfortunately, if an attribute you pass in to the social_share_button_tag is used depends on the platform you're trying to share on to, and each of them accept different params than the other. For example, Facebook doesn't allow you to specify images when you try to share that way. You can see the attributes that are passed into each of the platforms in the social-share-button.coffee file.

The best way to get the image to appear is to include meta tags for open graph which includes the title, description, image to use etc for that page. You can see all the relevant Open Graph Markup here. If you render those tags in the header section of your page when its rendered, those images and other information will appear when you share the page. For example:

<meta property="og:title" content="<%= @campaign.project.project_name %>" />
<meta property="og:description" content="<%= @campaign.project.description %>" />
<meta property="og:image" content="<%= @campaign.campaign_image_url %>" />

Additionally, it'll appear even if the user shares the page using the URL straight, rather than going through that plugin. This has become pretty standard now and other platforms such as Slack etc will also use those tags to determine how to display a link on their platform.

Navin Peiris
  • 2,526
  • 2
  • 24
  • 25
  • It has to be in the `` section of the rendered page @HaseebAhmad – Navin Peiris Mar 09 '17 at 10:41
  • Take a look at http://stackoverflow.com/questions/14929653/placing-facebook-metatags-in-rails-application to check out one approach of adding meta tags to the header in rails – Navin Peiris Mar 09 '17 at 10:43
  • let me do like that – Haseeb Ahmad Mar 09 '17 at 10:49
  • how i need to give links? – Haseeb Ahmad Mar 09 '17 at 21:21
  • If you mean the link to be shared on to a social platform, you can give that along with the call to `social_share_button_tag` like so: `<%= social_share_button_tag(@post.title, :url => "http://myapp.com/foo/bar") %>`. When it's shared, the image, title, description etc will now be parsed by the social platform using the `og:xxxx` markup on the page pointed to by that url – Navin Peiris Mar 10 '17 at 00:13