Restful Facebook canvas app with Rails and POST For Canvas

Posted: March 7th, 2011 | Author: | Filed under: Facebook | 12 Comments »

Update: The rack middleware is now included in Facebooker2. If you’re using Facebooker2, see the readme for details.

Starting March 10th, The POST for canvas option will become mandatory for all Facebook canvas apps. This means all requests to your iframe originating from the Facebook frame will be POST requests. If you use restful controllers in your app, this is not a good news. Fortunately, there is a very easy way to convert those POST back to GET with a Rack middleware :

Since all request coming from Facebook will include a signed_request parameter, you can assume that these requests are coming from the top frame. Other requests made directly from your iframe won’t be altered. You can safely now safely use Post for canvas and continue to assume all requests coming from the top frame will be get requests.


  • Pingback: Cookies and Facebook canvas apps | redirect_to :hack

  • Russell

    Thanks for this, I was trying to do something similar with the built in Rack::MethodOverride and was having issues.

    One thing, I don’t have a signed_request in my POST params, so I switched over to looking for ‘fb_sig_app_id’.

  • http://www.facebook.com/Koffaa Christopher Speer

    Thank you very much. But I cant get it to run with my application.
    My config.ru looks like this:

    require ::File.expand_path(‘../config/environment’, __FILE__)
    require ‘facebook_postfix’
    use Rack::FacebookPostFix
    run Eventknecht::Application

    If I change the order, “use” after “run” just brings up errors before the app even starts. Any idea?
    I am using Rails 3.0.1.

  • http://www.facebook.com/Koffaa Christopher Speer

    Never mind :)

    It’s just important to use the Rack Middleware BEFORE the run statement AND edit the code to make it work with request.POST['fb_sig_app_id'] or any other default facebook parameter if you dont have a signed_request parameter!

    Thanks a lot pal, this saved us a lot work!

    • http://coderubik.com Pierre Olivier Martel

      I’m glad you got this to work! And thanks for the follow up.

  • http://profiles.google.com/breathing James Young

    For some reason, this is still not working for me. I don’t need config.ru if i am using rails greater than 2.3, right? It looks like that it sends a get method and then a post method since my index action gets called in front of the create action.

    • http://coderubik.com Pierre Olivier Martel

      Unfortunately, I don’t know what’s the best way to set the rack middleware
      using Rails 2.3. I guess a google search should give you an answer.

    • http://www.socialcubix.com/services/mobile/iphone-application-development iPhone App Developers

      I love to visit and read like these sides

  • http://www.cygnismedia.com/mobile-phone-application/iphone-application.html iphone app developer

    its great man nice to see your post Martel

  • http://www.smartappsforkids.com/ app for

    Educational
    apps can be downloaded and makes the study a fun activity. These educational
    apps not only enhance the performance of the kid in academics but also help to
    develop the mental and conceptual thinking of the kid.

  • http://www.cygnismedia.com/ Facebook App Developers

    Is that working correctly for time line canvas?

    • http://coderubik.com Pierre Olivier Martel

      Yes, it should be just the same.