Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBrian Gilbank
9,586 PointsSocial Sharing Links Rails
I am trying to add social sharing links to my rails posts, without a gem. Here is my current code:
<!-- 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 - &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>
Facebook tries to open, then crashes and twitter gives me a page not found error. Any help would be appreciated. Thanks!
2 Answers
Brian Gilbank
9,586 PointsI Finally figured it out:
Posts Controller:
Social Sharing for Single Posts
def original_url
base_url + original_fullpath
end
View:
<!-- 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 - &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>
Here is the documentation: http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-original_url
Irving Amador
7,488 PointsMaybe you have to escape the the rails code, something like this:
$('some_element').replaceWith('<%= j render 'some/element_template' %>');
Here are some links about it: http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html http://stackoverflow.com/questions/1620113/why-escape-javascript-before-rendering-a-partial
Hope it helps.
Brian Gilbank
9,586 PointsThanks Irving, I will take a look.