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.

Disabling rails web site when using mongrel,Apache,capistrano 1.4.1

Problem
You want to disable your rails web site for maintenance, but your application uses an older capistrano version than the one currently installed.

Solution
According to the RubyOnRails Cookbook recipe 13.12, it should only be a case of running cap disable_web (enable_web).
But in the meantime you have upgraded your capistrano version to version 2, and started using mongrel server as well.
So if you are using virtual severs and proxy with mongrel, the first thing to do is add the following in your Apache configuration file:

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [R]

If you needed to make the above change then you will have to restart apache in your server.

Then in your local pc you can run the following:

export REASON="Maintenance for MySQL upgrade"
export UNTIL="Sat May 10 15:30:00 2008"
cap _1.4.1_ disable_web (to put it in maintenance)
cap _1.4.1_ enable_web (to restart it again)

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

Ext.ux.grid has no properties error

Problem
Trying to use ExtJS with RubyOnRails on Heroku, gives the following error and a blank page in Firebug.

Ext.ux.grid has no properties
...
Line 141

Solution
In Heroku, you will probably have to manually upload the files from the ExtJS library, as well as the ext_scaffold plugin.
When you do that, two files that ext_scaffold needs, and which they should be located in ext_scaffold/assets/javascripts should be copied to public/javascripts.
The files are:

ext_datetime.js
ext_searchfield.js

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

Installing sqlite3-ruby in Mandriva and Rails 2.0.2

Problem
After upgrading to Rails 2.0.2 when trying to install the sqlite3-ruby gem got the following error:

ERROR:  Error installing sqlite3-ruby:
ERROR: Failed to build gem native extension.
/usr/bin/ruby extconf.rb install sqlite3-ruby
checking for sqlite3.h... no
make
make: *** No rule to make target `ruby.h', needed by `sqlite3_api_wrap.o'.  Stop.
Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.1 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.1/ext/sqlite3_api/gem_make.out

Solution
In Mandriva you need to install the ruby-sqlite3 and the development libraries first, like:

sudo urpmi ruby-sqlite3
sudo urpmi libsqlite3-devel

After that you should be able to install the gem as normal.

Upgrading to Rails 2.0.2 problems/solutions

Problem
Upgrading from Rails version 1.2.3 to version 2.0.2.
Following the suggestion in DH’s weblog here when using sudo gem install rails -y, it gets the trunk version of rails which at the time was 2.0.2.9216.
This causes a few problems and breaks a few more things.

Solution
In order to get back to the latest stable version, you have to uninstall a few of the gems installed from the trunk version.
If you have installed the latest version of top of an existing one using gems, then doing:

gem list --local

will give you all the installed versions of the gems. Something like:

actionmailer (2.0.2.9216, 2.0.2, 1.3.3)
actionpack(2.0.2.9216, 2.0.2, 1.3.3)
rails(2.0.2.9216, 2.0.2, 1.2.3)
...

Make sure you uninstall all the gems with version number 2.0.2.9216:

sudo gem uninstall rails
sudo gem uninstall actionmailer
sudo gem uninstall actionpack
sudo gem uninstall activerecord
sudo gem uninstall activeresource
sudo gem uninstall activesupport
sudo gem uninstall rails

In each of the above commands the gem package manager should ask you to select the one you want to uninstall.
Pick the one with the 2.0.2.9216 (or other version later than 2.0.2).

At the end if you list the gems again you should only have gems going up to version 2.0.2

An extra step I had to take for making the migrations work was to install rake again:

sudo gem install rake

After all these my installation seems to be working fine again.

Regular expression checking of landline/mobile UK numbers in Ruby on Rails

Problem
You want to have validations for phone numbers in Ruby on Rails code.

Solution
The mobile numbers in UK should start with a 0, followed by 7, then a number between 5 and 9, and finally another 8 numbers.
For the landline numbers in UK the number should start with 0, followed by either 1 and 8 or 9 digits, or by 2 or 3 and then 9 digits.
So in order to use a regular expression in your validations, you could use something like the following in your model:

case phone_no_type

  when 'mobile'

    if phone_no !~ /\A(([0][7][5-9])(\d{8}))\Z/

      errors.add('Mobile number")

    end

  when 'landline'

    if phone_no !~ /\A([0])((([1])(\d{8,9}))|(([2-3])(\d{9})))\Z/

      errors.add('Landline number')

    end

  end

Red Box progress indicator with active_scaffold_upload branch

Problem
You need to display a splash screen when uploading files to a Ruby on Rails application.

Solution
My model:

class Number < ActiveRecord::Base
  file_column :intro
  file_column :vmail
end

The steps I have followed.

  1. Install redbox plugin from project path run:
    ./script/plugin install svn://rubyforge.org/var/svn/ambroseplugins/redbox
  2. If files redbox.js, redbox.css and redbox_spinner.gif haven’t been copied over to the public folders copy them over manually or go to vendor/plugins/redbox and run:
    rake update_scripts
  3. Find an animated gif for the progress indicator to be used (ie pleasewait.gif) and copy it over to public/images.
  4. In order to be able to use the progress indicator in different parts of the application, add the following to the app/views/layouts/ :
    application.rhtml
                 <%= stylesheet_link_tag 'redbox' %>
                <%= javascript_include_tag 'redbox' %>

    near the place where <= javascript_include_tag :default > is located.
    Also at the bottom of the page create the content of the redbox:

    <p id="redbox" style="display: none"> </p>
    
    <p style="color: blue"> Please wait ....
    
    Uploading files
    
    <img src="/images/pleasewait.gif" width="200" />
    
  5. Copy _update_form.rhtml, update_form.rhtml and update.rjs from vendor/plugins/active_scaffold_upload/frontends/default/views/ to the app/views/numbers (model you want to use).
  6. To display the redbox in the numbers/_update_form.rhtml add the following in the submit_tag:
     , :onClick => "return RedBox.showInline('redbox') " %>
  7. To close the redbox after the uploading was successful, add the following in numbers/update.rjs as the first line inside controller.send :successful? clause:
    page.RedBox.close()