Daily Glow Daily Glow - 6 months ago 24
Ruby Question

How to share a post in twitter using ruby on rails?

I am trying to share a post created in rails which is just a text content. Coding to share that post's title and url using this code

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


But i don't know what the problem is but when i click it nothing happen .
any one tell me whats the problem or suggest me some other way of code to share my post's title and comment.

Answer

You can use the pretty-social jquery plugin. Add this in a javascript file:

$('.prettySocial').prettySocial();

To include the plugin, you can paste the plugin file in your assets or vendor folder.

And this on your posts:

<div class="prettySocial" data-type="twitter" data-url="<%= request.original_url %>" data-description = "<%= @post.content %>" data-media = "<%= @post.main_photo %>"><%= image_tag("twitter.png")%>

You'll get a twitter icon, which will trigger a share onClick.