views:

470

answers:

3

I would like to respond to different formats in my Rails app:

respond_to do |format|
   logger.info "in main format block, request.format.fbml? = #{request.format.fbml?}"
   format.html # index.html.erb
   format.fbml # index.fbml.erb
end

This is simple. If facebook is requesting a page, return a .fbml.erb file, otherwise return a .html.erb file.

My app knows to send over main.fbml.erb from the layouts directory instead of main.html.erb but it never sends app/views/main/index.fbml.erb in the respond to block if I respond as shown above in my main controller's index action.

Anyone know how to easily do this?

I use the gem "facebooker" which sets the format to fbml automatically.

EDIT Output from my Rails log:

in main format block, request.format.fbml? = true
Rendering template within layouts/main
Rendering main/index
+1  A: 

Facebooker should do this automatically. It adds a set_facebook_request_format before filter to your controllers. If you'd prefer not to add the whole of facebooker to your app, it looks relatively easy to extract the functionality.

cwninja
Ah good to know that is done automatically. However, I use facebooker and the issue still remains. It renders the layout just fine but does not render anything in the respond_to block. Do you know why this could be happening?
Tony
A: 

Depending on the version of Facebooker that you are using, it may be necessary to explicitly add the following line to your /config/initializers/mime_types.rb

Mime::Type.register_alias "text/html", :fbml
Mike Buckbee
That did not seem to help
Tony
A: 

I just ran into this after a long time away from dealing with Facebooker and writing Facebook apps. Turns out, in my new app's Facebook settings, the render method was set to IFrame instead of FBML. It makes sense that facebooker won't send fbml to an iframe because a browser wouldn't know how to render and fbml tags.

John