Created
April 25, 2012 15:40
-
-
Save pheisiph/2490746 to your computer and use it in GitHub Desktop.
allow Facebook to access (some)content that is login-only normally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#app/views/layouts/facebook.html.erb | |
<!DOCTYPE html> | |
<html xmlns:fb="http://www.facebook.com/2008/fbml" | |
xmlns:og="http://opengraphprotocol.org/schema/"> | |
<head> | |
<title><%= yield(:title) %></title> | |
<meta charset="UTF-8"> | |
<meta property="og:title" content="<%= yield :og_title %>"> | |
<meta property="og:type" content="article"> | |
<meta property="og:image" content="<%= yield :og_image %>"> | |
<meta property="og:description" content="<%= yield :og_desc %>"> | |
<meta property="og:url" content="<%= yield :og_url %>"> | |
<%# <meta property="fb:admins" content=""> %> | |
<meta property="fb:app_id" content="<%= Settings.facebook.app.id %>"> | |
</head> | |
<body> | |
<%= yield %> | |
</body> | |
</html> | |
# and in app/views/posts/facebook.html.erb | |
<% content_for :title, @post.title %> | |
<% content_for :og_title, @post.title %> | |
<% content_for :og_image, @post.image.large.url %> | |
<% content_for :og_desc, @post.summary %> | |
<% content_for :og_url, post_url(@post) %> | |
<%= @post.summary %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/helpers/facebook_helper | |
module FacebookHelper | |
def facebook_spider? | |
/facebookexternalhit/.match(request.headers['HTTP_USER_AGENT']) ? true : false | |
end | |
def render_fb_or(&block) | |
if facebook_spider? | |
render :facebook, :layout => "facebook" | |
# render :nothing => true, :layout => "facebook" | |
else | |
yield if block_given? | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/posts_controller.rb | |
class PostsController < ApplicationController | |
include FacebookHelper | |
before_filter :find_post | |
before_filter :require_login | |
def show | |
# Use your default rendering code inside the block | |
render_fb_or { render } | |
end | |
private | |
def find_post | |
@post = Post.find(params[:id]) | |
end | |
def require_login | |
redirect_to root_url unless logged_in? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment