使用 Rails 3 进行粉丝门控的 Facebook 选项卡

发布于 2024-12-06 11:30:30 字数 2996 浏览 4 评论 0原文

我遵循了 CoverHound 的操作方法,请参阅 如何使用 Rails 和 JavaScript 创建粉丝门禁的 Facebook 选项卡

一切都很顺利,直到使用 Rails 应用程序为您的 FB 应用程序提供内容下的步骤 3 使用本地服务器进行预览时,出现错误。典型的 Rails 安全问题,只需添加

protect_from_forgery :except => :index

到我的 FacebookController 即可解决。从那时起,我就很成功了,在我的现场网站上对其进行了测试,并且工作顺利。现在添加门!

将 if 语句添加到我的 facebook index.html.erb 文件

<% if user_likes_page? %>
<div class="fans_tickets"></div>
<% else %>
<div class="like_tickets"></div>
<% end %>

中 在 FacebookHelper 中,我添加了以下内容来测试类似的内容,并解析来自 Facebook 的signed_request

def user_likes_page?
  fb_request = parse_signed_request
  return fb_request['page']['liked'] if fb_request && fb_request['page']
end

def parse_signed_request
  if params[:signed_request].present?
    sig, payload = params[:signed_request].split('.')
    payload += '=' * (4 - payload.length.modulo(4))
    data = Base64.decode64(payload.tr('-_','+/'))
    JSON.parse( data )
  end
rescue Exception => e
  Rails.logger.warn "!!! Error parsing signed_request"
  Rails.logger.warn e.message
end

在我的本地主机上测试时,它工作得很好,如果我不喜欢页面上,我看到带有信息的 div,告诉我喜欢该页面的比赛信息,当我喜欢该页面时,它会向我显示有关非粉丝 div 上提到的比赛的信息。伟大的!

将其发布到实时服务器,我收到一个显示错误页面。在我的日志中,我看到了这一点...

2011-09-23 12:38:29 INFO --   Processing by FacebookController#index as HTML
2011-09-23 12:38:29 INFO --   Parameters: {"signed_request"=>"-IIMtqxxUICCyfCOxpsBMvApiaLgEZAkr1tQltvK_bI.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImlzc3VlZF9hdCI6MTMxNjgwMzEwNywicGFnZSI6eyJpZCI6IjEzNzg2NDQ2NjMxMDQwOSIsImxpa2VkIjp0cnVlLCJhZG1pbiI6dHJ1ZX0sInVzZXIiOnsiY291bnRyeSI6ImNhIiwibG9jYWxlIjoiZW5fVVMiLCJhZ2UiOnsibWluIjoyMX19fQ"}
2011-09-23 12:38:29 WARN -- !!! Error parsing signed_request
2011-09-23 12:38:29 WARN -- undefined method `encoding' for #<String:0x7f845221da58>
2011-09-23 12:38:29 INFO -- Rendered facebook/index.html.erb within /layouts/facebook (37.4ms)
2011-09-23 12:38:29 INFO -- Completed   in 77ms
2011-09-23 12:38:29 FATAL -- 
ActionView::Template::Error (undefined method `[]' for true:TrueClass):
    1: <% if user_likes_page? %>
    2:  <div class="fans_flames_tickets"></div>
    3: <% else %>
    4:  <div class="like_flames_tickets"></div>
  app/helpers/facebook_helper.rb:4:in `user_likes_page?'
  app/views/facebook/index.html.erb:1:in `_app_views_facebook_index_html_erb___1213205873_70103145189880_0'
  app/controllers/facebook_controller.rb:6:in `index'

我似乎无法弄清楚是什么导致了这种情况,代码看起来并不依赖于实际服务器本身的任何内容...任何帮助将不胜感激!

哦,我没有继续“如何获取查看我的粉丝页面的访问者的 Facebook 用户 ID?”因为我不需要它,它只是为了类似的信息。



更新

通过调试弄清楚了......应该是显而易见的。我的本地主机正在运行 Guard 的情况下实现 JSON。由于 Guard 仅在我的开发模式下工作,因此在开发时安装了 JSON。安装在生产环境上并很快...

因为没有早点注意到这一点而感到有点愚蠢!感谢戴夫和戴夫的所有帮助。基思

I followed a how to by CoverHound see
How to Create a Fan-gated Facebook Tab with Rails and JavaScript
.

Everything went smooth up until step 3 under Serve the content for your FB App with a Rails application When previewing using my local server, I got an error. Typical rails security issue, simply solved by adding

protect_from_forgery :except => :index

to my FacebookController. From there I was golden, tested it on my live site and work swimmingly. Now to add the Gate!

Added the if statement to my facebook index.html.erb file

<% if user_likes_page? %>
<div class="fans_tickets"></div>
<% else %>
<div class="like_tickets"></div>
<% end %>

In the FacebookHelper I added the following to test for the like, and to parse the signed_request from Facebook

def user_likes_page?
  fb_request = parse_signed_request
  return fb_request['page']['liked'] if fb_request && fb_request['page']
end

def parse_signed_request
  if params[:signed_request].present?
    sig, payload = params[:signed_request].split('.')
    payload += '=' * (4 - payload.length.modulo(4))
    data = Base64.decode64(payload.tr('-_','+/'))
    JSON.parse( data )
  end
rescue Exception => e
  Rails.logger.warn "!!! Error parsing signed_request"
  Rails.logger.warn e.message
end

When tested on my localhost, It works great, if I haven't liked the page, I see the div with the info telling me to like the page for the contest info, when I have liked the page it shows me the info about the contests mentioned on the non-fan div. Great!

Post it to the Live server and I get an error page that shows up. In my Logs I see this...

2011-09-23 12:38:29 INFO --   Processing by FacebookController#index as HTML
2011-09-23 12:38:29 INFO --   Parameters: {"signed_request"=>"-IIMtqxxUICCyfCOxpsBMvApiaLgEZAkr1tQltvK_bI.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImlzc3VlZF9hdCI6MTMxNjgwMzEwNywicGFnZSI6eyJpZCI6IjEzNzg2NDQ2NjMxMDQwOSIsImxpa2VkIjp0cnVlLCJhZG1pbiI6dHJ1ZX0sInVzZXIiOnsiY291bnRyeSI6ImNhIiwibG9jYWxlIjoiZW5fVVMiLCJhZ2UiOnsibWluIjoyMX19fQ"}
2011-09-23 12:38:29 WARN -- !!! Error parsing signed_request
2011-09-23 12:38:29 WARN -- undefined method `encoding' for #<String:0x7f845221da58>
2011-09-23 12:38:29 INFO -- Rendered facebook/index.html.erb within /layouts/facebook (37.4ms)
2011-09-23 12:38:29 INFO -- Completed   in 77ms
2011-09-23 12:38:29 FATAL -- 
ActionView::Template::Error (undefined method `[]' for true:TrueClass):
    1: <% if user_likes_page? %>
    2:  <div class="fans_flames_tickets"></div>
    3: <% else %>
    4:  <div class="like_flames_tickets"></div>
  app/helpers/facebook_helper.rb:4:in `user_likes_page?'
  app/views/facebook/index.html.erb:1:in `_app_views_facebook_index_html_erb___1213205873_70103145189880_0'
  app/controllers/facebook_controller.rb:6:in `index'

I can't seem to figure out what would be causing this, the code doesn't look to be relying on anything from the actual server itself... Any help would be appreciated!

oh and I didn't continue on with the "How do I get the Facebook userID of the visitor viewing my fan page?" as I don't require it, its just for the like information.

UPDATE

Figured it out with debugging... should have been obvious. My Local host was implementing JSON with Guard running. With Guard only working in my development mode, JSON was installed on on development. Installed on Production and presto...

Feel kinda dumb for not having noticed this sooner! Thanks for all the help Dave & Keith

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掀纱窥君容 2024-12-13 11:30:30

我非常确定 undefined method 'encoding' for #<String... 错误与在 ruby​​ 1.8.7 与 1.9.2 或更高版本上运行应用程序有关。也就是说,您的 Live 服务器似乎正在运行 1.8.7 或可能是 Ruby Enterprise Edition。

如果可以的话,将您的实时服务器切换到 ruby​​ 1.9.2 版本,看看是否可以解决问题。否则这个周末我会多挖一点。

干杯!

-千瓦

I'm pretty sure the undefined method 'encoding' for #<String... error has to do with running the app on ruby 1.8.7 vs. 1.9.2 or higher. i.e. it looks like your Live server is running 1.8.7 or possibly Ruby Enterprise Edition.

If you can, switch your live server to a ruby 1.9.2 version and see if that fixes it. Otherwise I'll dig in a little more this weekend.

Cheers!

-kw

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文