ERROR: While executing gem … (ArgumentError)

Problem
After a recent upgrade to a newer rails version the gem package manager seems to be broken. Everytime you try to use gem install gem_name, you get the following error:

ERROR:  While executing gem ... (Gem::GemNotFoundException)

Deleting the cached files as suggested in other posts results in the error:

ERROR:  While executing gem ... (ArgumentError)

Solution
As suggested here, you need to do:

gem install rubygems-update
update_rubygems

which should be updating the gem version to the latest one, ie 1.3.0

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

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

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”;

ruby incorrect encoding of pound sign(£)

Problem
You want to pass the £ sign to an http service, but the ruby CGI.escape encodes it incorrectly.

Solution
After using ruby’s CGI.escape for the string as:

sms_msg_tmp=CGI.escape(sms_code)

then replace the encoding with the pound sign encoding as in:

sms_msg=sms_msg_tmp.gsub('%C2%A3','%A3')

It should then pass the correct value for the £ sign.

git: fatal error: `chdir’ failed: permission denied.

Problem
Trying to use the heroku gem to clone a project and do local modifications I got the following error:

git: fatal error: `chdir' failed: permission denied.

Solution
It turns out that in Mandriva, when using:

sudo urpmi git

what gets installed is the ‘GNU Interactive Tools’ that has nothing to do with the git version control system.
So make sure you first uninstall the git installed:

sudo rpm -e git

and then install the Git – Fast version control system, by doing:

sudo urpmi git-core

You should then be able to clone a heroku application:

heroku clone myapp

Converting Paradox tables to MySQL sql in Linux

Problem
You want to convert some legacy tables created in Paradox (.db, .px) to another format so you can use it in MySQL.

Solution
Download the px tools from here.

Follow the instructions, in the INSTALL file after you untar the file.
You should have to do the usual three step linux installation:

configure
make
sudo make install

Afterwards to make sure that the .db file is a paradox file run:

pxinfo -f  path/to/paradox/db/file.db

The program should read the header and report back with something along the lines:

File-Version: Paradox 7.x
Filetype: indexed .DB
....

To export each Paradox db file to an sql statement run the following:

pxsqldump -d mysql -f path/to/paradox/file.db > path/to/mysql/exported/file.sql