2012-03-18 4 views
0

Facebookのボットの特別なデータ用にレンダリングしたい。私は自分のページのコントローラにrails before_filter、facebookユーザエージェント、および動的OpenGraphデータ

<!DOCTYPE html><html><head> 
<meta property="og:title" content="<%= @og[:title] %>"> 
<meta property="og:type" content="website"> 
<meta property="og:url" content="<%= @og[:url] %>"> 
<meta property="og:description" content="<%= @og[:description] %>"> 
<meta property="fb:admins" content="100000235955045"> 
</head><body></body></html> 

そして、これをog.html.erbています

# encoding: UTF-8 
class PageController < ApplicationController 
    def index 
    @og = { :title => 'index title', :description => 'index desc', :url => 'http://someurl.com/index' } 
    if request.headers['HTTP_USER_AGENT'].eql? 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)' 
     render '/og', :layout => false 
    end 
    end 

    def cv 
    @og = { :title => 'cv title', :description => 'cv description', :url => 'http://someurl.com/cv/' } 
    if request.headers['HTTP_USER_AGENT'].eql? 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)' 
     render '/og', :layout => false 
    end 
    end 
end 

それを乾燥させるためにどのように私はできますか?私はヘルパーまたはbefore_filterを使用する必要がありますか? 私はbefore_filterのユーザーになりますが、コントローラから@og hashを設定する方法はありますか?

答えて

2

私はそれはgreateのですが、私はrender_ogメソッドの引数としてOG設定することができます

# encoding: UTF-8 
class PageController < ApplicationController 
    def index 
    @og = { :title => 'index title', :description => 'index desc', :url => 'http://someurl.com/index' } 
    render_og 
    end 

    def cv 
    @og = { :title => 'cv title', :description => 'cv description', :url => 'http://someurl.com/cv/' } 
    render_og 
    end 
end 
+1

、その後、あなたのページのコントローラーはこのなりますが、このよう

def render_og if request.headers['HTTP_USER_AGENT'].eql? 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)' render '/og', :layout => false end end 

をApplicationControllerにでメソッドを定義することができると思います? –

+0

問題が解決しました!ありがとうございました –