FBML Error (line 5): illegal tag “body” under “fb:canvas”

Problem
When trying to use facebooker according to the Developing facebook platform applications with rails book, in the network_test step you are getting the following error:

FBML Error (line 5): illegal tag "body" under "fb:canvas"

Solution
It should be caused because you are using the facebooker gem instead of the plugin.
Install the plugin:

ruby script/plugin install git://github.com/mmangino/facebooker.git

Running script/runner in production environment

Problem
Following from a previous post about email scheduling with runner and cron, it turns out that the runner default behaviour is to run in the development environment.

Solution
Although by reading the help for the script/runner, there is a suggestion to run it with the -e production added to the end, it doesn’t seem to be working.

The solution to make it running in the production environment was to delete the first line (shebang) from step 3 on this post

#!/usr/bin/env /path_to_your_app/script/runner

and then use the following in the cron setup:

RAILS_ENV=production /path/to/your_ror_project/script/runner /path/to/your_ror_project/lib/email_scheduler.rb

Have a look on paragraph Alternative Usage here

Dreamhost, Rails 2.1.1, Freeze, Capistrano

Problem
With the imminent upgrade in Dreamhost to Rails 2.1.1 (dreamhost blog post here), you may want to freeze your Rails version to a previous version, using capistrano.

Solution
On your local development pc freeze the rails:

rake rails:freeze:gems

Add the new code to your svn repository:

svn commit -m 'freeze rails'

And then deploy to dreamhost using capistrano:

cap deploy

Your rails project located on dreamhost should be frozen to your current Rails version.

Developing in Rails 2.1 and deploying in Rails 2.0.2 (or error – undefined method time_zone)

Problem
You are developing on the latest version of Rails (2.1), but your production server for deployment uses version 2.0.2 (as dreamhost is using at the moment).

Solution

  1. First change the environment.rb file to use the rails version in your deployment server
    RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION
    
  2. You should probably be geting the error: undefined method = time zone by now, so make sure you comment out from further down your environment.rb file the line:
    config.time_zone = 'UTC'
  3. Some further errors would be caused by the file config/initializers/new_rails_defaults.rb, so make sure you comment out the following lines:
    ActiveRecord::Base.include_root_in_json = true
    ActiveRecord::Base.store_full_sti_class = true
    ActiveSupport.use_standard_json_time_format = true
    ActiveSupport.use_standard_json_time_format = true

You should be able to deploy and use your application now.

Ruby on Rails email scheduling using runner and cron

Problem
You want to send emails from a Ruby on Rails application, when there is a specific condition on a database table. If the database table gets modified by another application outside Rails you cannot use an observer model.

Solution
We already assume that:

  • You are using a database
  • You have a model named voicemail (id, number_id, audio, created_at, updated_at)
  • You have a model named number (id, voicemail_email_set, voicemail_email, ….)
  • A mail server to use (smtp in our case)
  • Another application (voice application) populates the voicemail table but with empty updated_at values

So the steps we have to follow are:

  1. Change the settings in your config/environment.rb file to use the settings for your mail server, and make sure you restart your application after the changes:
    ActionMailer::Base.smtp_settings = {
      :address        => "yourmailserver.com",
      :port           =>  25,
      :domain         => "your.domain.com",
      :authentication => :login,
      :user_name      => "your_smtp_username",
      :password       => "your_smtp_password",
      :raise_delivery_errors  => true}
  2. Create your mailer model (ie voicemail_mailer.rb), in app/models:
    class VoicemailMailer < ActionMailer::Base
       # We need the open-uri to be able to open url *** if the file to attach is in an http location ***
      require 'open-uri'
     
      def sent(email_to,email_from,email_subject,email_body,voicemail_to_send)
        # Check to see if we have a file for the email body message
        @subject    = email_subject
        @body       = email_body
        @recipients = email_to
        @from       = email_from
        @sent_on    = Time.now
        
        # Split the file in directory and filename
        file_path = File.split(voicemail_to_send)
        file_dir  = file_path[0]
        file_name = file_path[1]
        
        # Get the file
        tmp_file = open(voicemail_to_send).read
        
        part( :content_type => "application/wav", 
              :disposition => "attachment; filename=#{file_name}", 
              :transfer_encoding => "base64") do |attachment|
                attachment.body = tmp_file
        end
      end
    end
    
  3. Create your email scheduler in file lib/email_scheduler.rb:
    #!/usr/bin/env /path_to_your_app/script/runner
    
    # get all the voicemails that have not been sent yet 
    voicemails_to_email = VoiceMail.find(:all, :conditions => 'updated_at is null')
    
    # For all the voicemails we have, send them and update the field date_sent
    for vm2email in voicemails_to_email do
      # Get the number for the voicemail
      number = Number.find(vm2email.number_id)
      
      # check to see if the send to email is set for the number
      if number.voicemail_email_set
        # Get number details (email_to,email_from etc)
        email_to          = number.voicemail_email
        voicemail_to_send = vm2email.audio
        # Set other details
        email_from      = 'Service@yourdomain.com'
        email_subject   = 'Please find attached your voicemail message'
        email_body      = "Received on: #{Time.now} \n for number: #{number.phone_no}"
    
        # Now send the email
        VoicemailMailer.deliver_sent(email_to,email_from,email_subject,email_body,voicemail_to_send)
    
        # And update the record's date_sent field
        vm2email.updated_at = Time.now
        vm2email.save
      end
    end
    
  4. Create a task in your crontab that runs the scheduler (every five minutes):
    0,5,10,15,20,25,30,35,40,45,50,55 * * * * path_to_your_ror_app/lib/email_scheduler.rb

Rails 2 new ways

Problem
To get used to the new ways of working with Rails 2.

Solution
Here are some of the new ways of doing things in Rails 2.

  1. Create a scaffold and migration on one step:
    ./script/generate scaffold product fied1_name:string field2_name:string field3_name:float

    Make sure that you don’t have any previous migration(ie manually created) for the same model, as the generator will stop and won’t overwrite the previous migration.

Upgrading Ruby on Rails application from 1.2.3 to 2.0.2

Problem
Upgrading an existing Ruby on Rails application from 1.2.3, to 2.0.2, presents few problems. I will try and keep a record of the ones I encounter along the way, here.

Solution

  1. Change the config/environment.rb to let the application know to use the 2.0.2 gem rail version,
  2. change the following line from:

    RAILS_GEM_VERSION = '1.2.3' unless defined? RAILS_GEM_VERSION

    to:

    RAILS_GEM_VERSION = "2.0.2" unless defined? RAILS_GEM_VERSION
  3. Run the following to generate the secret key for the application:
  4. rake secret
  5. Copy the magic key in a new section in your config/environment.rb as in:
  6. ...
    
        # config.log_level = :debug
    
        # Your secret key for verifying cookie session data integrity.
        # If you change this key, all old sessions will become invalid!
        # Make sure the secret is at least 30 characters and all random,
        # no regular words or you'll be exposed to dictionary attacks.
        config.action_controller.session = {
          :session_key => '_yourapplication_session',
          :secret      => 'long_string_generated_from_rake_secret'
        }

Google Analytics UID

Problem
You want to find out your UID for a specific domain that you have set up with google analytics.

Solution
When you first create the tracking code for your website, it’s quite obvious how to get the UID for that specific website.
If you decide later on to change your site, from drupal to wordrpess for example, it’s not quite clear where to get this UID, to use it with the new tracking plugin in the new site (which still uses the same domain name).
After some searching it turns out that to be able to find it you have to do the following:

  1. Log in to your Google Analytics Account
  2. Click on the ‘Edit’ link under Settings for the domain you want to find the UID
  3. In the new page click on the link ‘Check Status’ after Receiving Data at the top
  4. Your UID should be in the text box with something like _uacct = “UA-xxxxxxx-x”;