Rails 3 check if the user has specified request.format

The request.format object in Rails is always set. If the user has not specified a value in their URL, e.g. example.com/projects.json, then it will default to :html.

To check if the user has chosen the request format, use this piece of code:


Fix Fast Debugger (ruby-debug-ide) on Windows 7

I've finally found a solution that will make the Fast Debugger (a.k.a ruby-debug-ide) work with Netbeans 6.9.1+ on Windows 7. This solution should also work for anyone using Vista, which I know from experience has similar issues.

First step: make sure the Ruby DevKit is installed. Full instructions for installing it are here on Github: https://github.com/oneclick/rubyinstaller/wiki/Development-Kit.

The DevKit basically temporarily enhances Windows and allows tricky Gems to be compiled.

Second step: Next you must install the correct debug Gems. If your Ruby version is 1.9.2+ then you will require the gems ending with a "19".

The way to make the gems install successfully is through Netbeans. Go to Tools > Ruby Gems > New Gems.

Do a search for "ruby-debug" and you shall see a list similar to this:

Install the "latest" version of all of the Gems which I have highlighted. Do not try and install "ruby-debug".

Third step: Once the Gems have installed, open up and new Command Prompt, run the command "gem list" and check that all of the Gems are listed, if not try to install them manually.

Next, the "rdebug-ide" file must be edited. Find this by browsing to (or similar):


Find the line (around 79):

Debugger::PROG_SCRIPT = ARGV.shift

Replace it with:

script = ARGV.shift
Debugger::PROG_SCRIPT = (script =~ /script([\\\/])rails/ ? Dir.pwd + $1 : '') + script
puts Debugger::PROG_SCRIPT
#Debugger::PROG_SCRIPT = ARGV.shift

Final step: Save the file and restart your computer. When Netbeans is reopened, click the "Debug main project" button to start the server in debug mode.

Thanks must be given to the following for help in figuring this one out:

Fix Rails error: The specified module could not be found: mysql-2.8.1-x86-mingw32 mysql_api.so (LoadError )

After updating my Rails version to 3.0.7 I have been experiencing the error:

The specified module could not be found. - C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql-2.8.1-x86-mingw32/lib/1.9/mysql_api.so (LoadError )

This occurs every time I try to load the Webrick server.

The solution is to find the "libmysql.dll" file and copy it to your Ruby bin.

The location of these folders on Windows 7 is:
C:\Program Files\MySQL\MySQL Server 5.5\lib

As soon as that DLL is copied you can start the server without any problems.

Sort string array values as integer with PHP

Sometimes you'll want to sort an array by its values. Normally you can use one of the PHP sort functions such as sort() or usort(). This will sort your array into ascending order for either integer, starting with zero or string, from A - Z.

However, you may have the following array:

$scores = array(

If you try to sort these by the ID (28, 32, 48) the sort function will not take just the numbers into account. It will also then sort on the string, from A to Z.

There's a simple way to fix this. We need to upgrade the usort() function by adding an extra argument. We'll add the SORT_NUMERIC predefined constant as the second argument and magically PHP will ignore sorting on the string part of the array values and instead successfully sort the array by the ID.

Using the following code will output the array in the order: 2, 0, 1 as expected.

usort($scores, SORT_NUMERIC);
//0 = 28-Joe
//1 = 32-Andy
//2 = 43-Steve

Detect if HTML 5 Geolocation is Enabled

If you want to check whether a browser has HTML 5 geolocation support enabled and display a warning message to the user if it is not, then this function should help you out.

function display_nosupport_message()
		$('#map_canvas’).text('Your browser does not support HTML 5 geolocation. ');

In this example I check to see if the navigator.geolocation method is available. If not then I use a jQuery command to update the div that would normally house the Google Map by adding warning text for the user.

Easy Javascript crossfading with jQuery

In today’s World Wide Web many websites are featuring projects or artwork on their homepage via a crossfading gallery. In the past this was normally done by an Adobe Flash movie but these days it is usually much easier to implement with Javascript.

A Javascript solution provides many benefits such as cross browser support, no plugins required and highly improved performance. There are many different ways to implement a crossfade in Javascript, many of them very complex, so today I will be showing you how this effect can be achieved with the jQuery framework, Cycle plugin and only a few lines of code.

First of all you must include the jQuery JavaScript by either downloading it from jQuery or including it right from their website.

Next we must download the Cycle plugin and include it from your computer.

Next step is to define your slideshow files in HTML. Simply write your img tags into a slideshow div tag.

Finally all you have to do is include an inline Javascript function to initiate the cycler. Remember to match up the class name of the slideshow div with the class name in the function.

<script type="text/javascript">
$(document).ready(function() {
fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...

That's it, a Javascript crossfading gallery in minutes. To see examples and additional effects that the cycle plugin can perform, please see the cycle plugin website.

Unescape HTML output in Rails 3 using html_safe

I've been having the problem of trying to output generated HTML into a ruby view template in a similar fashion to this:

 <%= generated_html %>

The problem is in Rails 3 you no longer need the "h" function which escapes HTML. This is now done automatically.

The solution is to tell rails that this is safe HTML and you want to unescape the output using the "html_safe" function as follows:

 <%= generated_html.html_safe %>

Fix NetBeans "db:migrate" task does not exist

I have recently reinstalled Ruby on Rails 3.0.0 and created a new Rails project in NetBeans 9.6.1

I could not migrate my database, or any other rake tasks for that matter. I kept getting the error: "db:migrate" task does not exist

The solution is to right click your project select "Run/Debug Rake Task" and then click the button titled "Refresh Tasks".

Once this is done you should see a list of all your rake tasks. After this you can simply re-run the db:migrate task and it will work.

PHP Installer doesn't ask for web server

If you are having the problem that the PHP Installer MSI is not asking for your web server type and therefore not updating your server configuration or not installing the correct libs such as "php5apache2_2.dll" then you are probably using the wrong MSI.

All of the MSI installers are on the http://windows.php.net/download/ page. You do not want to use the VC9 installers. You need to use the VC6 x86 Thread Safe installer. This is usually the last option, per version.

The link to the latest safe MSI is here: PHP 5.3 VC6 Thread Safe MSI

Fix "A connection attempt failed because the connected party did not" PHP error

I recently upgraded my PHP version to 5.3 and afterwards could not connect to MySQL. I was getting the following error in my PHP error log: PHP Warning: mysql_connect(): [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306)

The solution is to do with Windows enabling IPv6 support while MySQL does not support this yet. 'localhost' is being converted to ::1 instead of, the solution to this is simple.

Go the the following directory: C:\Windows\System32\drivers\etc

Allow yourself full access to the hosts file by right clicking it and clicking properties.

Next go to the security tab and click the big "Edit" button.

Click on the "Users" group. Below this check the "Full Control" box and then save the changes.

Now that you have access to the file, open the hosts file with a text editor such as notepad.

Comment out the ::1 line with a # so the file will look something like:

# Copyright (c) 1993-2006 Microsoft Corp.
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
# For example:
#     rhino.acme.com          # source server
#     x.acme.com              # x client host       localhost
#::1             localhost

Once you are done, save the file and try to access your PHP page again that is using MySQL. It should work immedietley.

© Blake Simpson, 2012 – 2018