Archive for Rails

Facebook Iframe Application: Resize Iframe

If you are using Rails and Facebooker, and you have a resizable Iframe application and you are having problems with resizing, do the following:

a) Make sure that the Iframe is set to Resizable in the Facebook appplication settings.

b) Instead of calling just <%= init_fb_connect "XFBML" %> (as mentioned in all the tutorials), use

<% init_fb_connect "XFBML" do %>
FB.CanvasClient.startTimerToSizeToContent();
<% end %>

This should automatically resize your Iframe in the facebook canvas

Leave a Comment

Facebooker: Creating an Iframe Application (Incorrect Signature)

I started developing a Facebook app for one of my existing websites.  Since, I already had a working website and I prefer normal html and js over fbml and JS over FBML and FBJS, I decided to take the IFrame route. While working with my app, I ran into this bizarre problem where clicking on any link after loading the facebook app, gave me an invalid Signature exception. After digging my way through the Facebooker (Rails plugin for Facebook app dev) code, I find that the expected signature is nil inside the "verify_signature function". Turns out you need to append all your links in a Facebook iframe app with the fb_sig params. So, I created a function in Application.rb and set it as a before_filter

before_filter :set_facebook_params
ensure_application_is_installed_by_facebook_user
 
protected
def set_facebook_params
@fb_params = params.inject({}) do |collection, pair|
collection[pair.first] = pair.second if pair.first =~ /^fb_sig/
collection
end
end

The above code will extract out all the fb_sig params from the url and store it in @fb_params. Now add this hash to every url that you create inside you Iframe. This was the fix that I came in the middle of the might, and I am sure this may not be the best fix. But this works. Let me know if there is another workaround.

Leave a Comment

SMERF and issues with MySQL

SMERF is the meta form generator plugin for Rails. The plugin I would say is very useful. It takes in a form definition file, defined in YAML and creates a form on the fly. These forms are associated to users and the responses to the forms are also persisted in the db. Ideal for making a quick survey for the users on your websites. I was using this plugin to create a survey for the upcoming fashion website chictini.com, and ran into a weird issue.

SMERF plugin, actually parses a form definition YAML file, and creates a SmerfForm object out of it. This SmerfForm object is actually persisted in your db, for fast future accesses. Now this SmerfForm object is serialized into a text field in a MySQL table. Everything works fine when you have small forms. But as soon as your forms get bigger, you hit a MySQL issue. The max size of a text field in MySQL is 64 KB. So if the serialized data is more than 64 KB, you start getting weird errors. The serialization does nnot throw an error, neither does saving the object in DB. But when you try to access the serialized data, you basically get incomplete serialized data, which is nothing but garbage for your purposes.

Solution:  Change the column which stores the serialized data to longtext for MySQL. You can create a migration for this which would look like this:

 
class UpdateColumnInSmerf > ActiveRecord::Migration
def self.up
execute "ALTER TABLE smerf_forms MODIFY COLUMN cache LONGTEXT"
end
 
def self.down
execute "ALTER TABLE smerf_forms MODIFY COLUMN cache TEXT"
end
end
 

This was was one of the annoying issues which kept me awake till 3 am.

Comments (1)

Div inside a p : Invalid HTML

I was trying to center an element using the p tag. Like :
<p style="margin-left:auto; magin-right:auto; width: 50 em;">
<%= show_simple_captcha %>
</p>

show_simple_captcha is a helper method from the simple captcha rails plugin. This view helper generates a div which helps in captcha validation. Now, this created a problem. This piece of code started to behave randomly in IE. Even the source generated in Firefox is invalid HTML. After much scratching arnd, I found that <div> tags are not allowed inside <p> tags. Strange !! I dont have a reason of the top of my head as to why this is disallowed, but this is invalid W3C HTML.

Leave a Comment

action_mailer_tls and Ruby 1.8.7

If you are using action_mailer_tls rails pluin and you shifted to Ruby 1.8.7, your app might break. I recently ran into this problem, coz my host providers shifted to Ruby 1.8.7 and I started getting errors when I was trying to send emails. The problem is because the check_auth_args function has changed in Ruby 1.8.7. Now it requires only 2 args as opposed to 3. The action_mailer_tls plugin uses this function but uses three arguments. So, to get back running again, make the following changes:

1).  Go to your action_mailer_tls plugin installation
2). Inside the do_start function, replace the call to "check_auth_args" with the following snippet:

 
no_args = method(:check_auth_args).arity
if no_args == 2
  check_auth_args user, secret if user or secret
else
  check_auth_args user, secret, authtype if user or secret
end
 

Comments (3)

Ruby 1.8.7 String#chars compatibility problem with rails

Recently, I started getting weird "LoadError" with my Rails app on Dreamhost. I had an "Article" model which contained many "Photo" models. When the "Article" controller tried to refer to the photos in its object, I got the following error:
LoadError: Expected {full server path}/photo.rb to define Photo.

I had a photo.rb file which had class Photo defined. I was using attachment_fu plugin to implement photos. After some head scratching, I found that the error was in photo.rb file. Hence photo.rb was not loading correctly, and therefore a load error.

The actual error happened coz the Dreamhost guys had updated Ruby to 1.8.7 which has some incompatibility with Rails 2.0.2 that I was using. When I tried to create a Photo object on the command line, I got this error:
NoMethodError: undefined method '[]' for #<Enumerable:enumerator></code>

Ruby 1.8.7 has String#chars. This returns an Enumerator object but Rails 2.0.2 expects an ActiveSupport::MultiBye::Chars object.

We can avoid this conflict, by putting the following code in the config/initializers/ directory.

  1.  
  2. unless '1.9'.respond_to?(:force_encoding)
  3. String.class_eval do
  4. begin
  5. remove_method :chars
  6. rescue NameError
  7. # OK
  8. end
  9. end
  10. end
  11.  

Comments (3)

Insert comments in an erb file

How does one insert comments in an erb file. The normal http tag to insert comments do not work well when there are embedded ruby statements in the html.

<!-- <b>Hello</b><%= @article.author %> -->

The above statement will not stop the evaluation of article.author. Instead, @article.author will still be evaluated and placed as a comment in the generated html file. Say the author was Balpreet, then we have a html comment:

<!-- <b>Hello</b>Balpreet -->

So, what if we want to just comment stuff out, so that the embedded ruby statements are not evaluated at all. There are basically two ways that I think we can achieve this:

1. Insert block level comments using embedded ruby.
<%
=begin %>
<b>Hello</b><%= @article.author %><%
=end %>

2. Insert conditional
<% if false %>
<b>Hello</b><%= @article.author %>
<% end %>

Leave a Comment