join stephan.com
Back

Really Easy 'Facebook Like' for Rails

18JUL10

I don’t think anyone noticed, but I’ve had Facebook Like done wrong on my site for a few weeks. I had pasted in their boilerplate code, not noticing that the URL was encoded in there already as example.com. Duh.

I made myself a little helper that should work well for everyone. It automagically inserts the current page (using request.url) and encodes it for you. Just put this in your application_helper.rb

          def facebook_like
            content_tag :iframe, nil, :src => "http://www.facebook.com/plugins/like.php?href=#{CGI::escape(request.url)}&layout=standard&show_faces=true&width=450&action=like&font=arial&colorscheme=light&height=80", :scrolling => 'no', :frameborder => '0', :allowtransparency => true, :id => :facebook_like
          end
        

Then on any page you want Facebook Like, just include this:

<%= facebook_like %>

Easy! Go ahead and ‘like’ this page, please :)


You must join stephan.com to comment. Join now!