"Show me the page" fails to open a browser window

If you get an error like this:

Unable to launch /home/bruce/Projects/myproject/tmp/capybara/capybara-201110311210111407691101.html 

... update your launchy gem. It failed for us in version 0.4.x. We could fix the issue by upgrading to 2.0.5.

Rails 3.1 error message: Could not find a JavaScript runtime

After starting the Rails server in a freshly generated Rails 3.1 project you could see an error message such as

/usr/lib/ruby/gems/1.8/gems/execjs-1.3.0/lib/execjs/runtimes.rb:50:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

Just add a JavaScript runtime to your Gemfile and the error vanishes.

Examples:

gem 'therubyracer'
gem 'extjs'

Web font embedding requires new CSS for IE9

If you embedded web fonts in the past years (e.g. by copying CSS from a Font Squirrel @font-face kit), that CSS won't work in Internet Explorer 9.

You can fix it by turning these styles...

@font-face
  font-family: 'MyFont'
  src: url('myfont.eot')
  src: local("☺"), ('myfont.ttf') format("truetype")
  font-weight: normal
  font-style: normal

... into these:

@font-face
  font-family: 'MyFont'
  src: url('myfont.eot')
  src: local("☺"), url('myfont.e...

How the Clearance gem remembers and clears sessions

Clearance is a gem that provides authentication functionality (e.g. login, logout). This note explains, how the clearance login, logout and (in old Clearances) remember me functionality works.

Login

Clearance defines a database column called "remember_token". When you login in, that token will be saved in a cookie. For that reason you don't have to re-sign-in when you close and open the browser again.
This also means that you can be logged in in more than a single browser. Also see [When ses...

Change the MySQL default character set on Amazon Relational Database Service (RDS)

Look here for informations how you can show the MySQL default character set.

At first you need the Amazon RDS Command Line Toolkit

  • download and unzip the [Amazon RDS Command Line Toolkit](http://aws.amazon.com/developertools/A...

Improve web font rendering in Windows by autohinting fonts

Web fonts are awesome. After being restricted to Arial for two decades there is finally a cross-browser way to embed fonts into web pages.

Unfortunately while web fonts look awesome on Linux and MacOS, they look horrible on Windows, a problem that gets worse with smaller font sizes.

The culprit is something called font hinting:

...

Connect to a VirtualBox guest machine over the network

You must reconfigure the guest so it will get its own IP address:

  • Shutdown the guest
  • In the guest's VirtualBox settings, choose Network and switch the network adapter type to Bridged
  • Restart the guest
  • Read the guest's IP address from the output of ifconfig (Unix) or ipconfig (Windows)
  • You can now connect to this IP address using HTTP, SSH, etc.

Note that by doing this your VirtualBox guest is fully exposed on the local network, just like your host machine.

Make Type 1 fonts (.pfb/.pfm) appear in OpenOffice on Ubuntu Linux

When you install Type 1 fonts (like makandra's corporate typeface), they won't show up in OpenOffice. OpenOffice requires some additional files to process them. Stefan Gründel found out what to do.

There is a tool that can produce the missing files:

sudo apt-get install t1lib-bin

Now you can create .afm files:

cd ~/.fonts
type1afm *.pfb

Restart OpenOffice and the fonts should show up.

Give table columns equal width using CSS

If you want to distribute a <table>'s width equally over its columns, you can use the following CSS property:

table-layout: fixed

It is supported by every browser ever.

Cucumber.yml was found, but could not be parsed.

If you encounter the error message above when running cucumber, just execute...
rm rerun.txt
...in the Rails directory.

Or run...
tests
...from the geordi gem. This will do the work for you automatically.

/proc/cpuinfo on Solaris

When used to do:

$> cat /proc/cpuinfo 
processor : 0 
vendor_id : GenuineIntel 
cpu family : 15 
model : 4 
model name : Intel(R) Xeon(TM) CPU 3.00GHz 
stepping : 1 
cpu MHz : 2992.785 
[...]

Instead, on Solaris, use psrinfo(1M)

$> psrinfo -v 
Status of virtual processor 0 as of: 09/17/2006 21:45:32 
on-line since 08/24/2006 16:16:03. 
The i386 processor operates at 1800 MHz, 
and has an i387 compatible floating point processor. 
Status of virtual processor 1 as of: 09/17/2006 21...

Stub methods on any instance of a class in Rspec 1 and Rspec 2

RSpec 1 (Rails 2)

With the most recent spec_candy.rb helpers you can say:

User.stub_any_instance(:foo => :bar)
user = User.new
user.foo
# => :bar

RSpec 2 (Rails 3)

RSpec 2 comes with this feature built in:

User.any_instance.stub(:foo => :bar)
user = User.new
user.foo
# => :bar

RSpec 3
-------...

Solve screen error "Cannot open your terminal '/dev/pts/0' - please check"

When using the screen tool you may be unable to start a screen session but instead encounter an error:

Cannot open your terminal '/dev/pts/0' - please check.

This is because another user (you) initiated the current terminal -- you probably did a sudo su into the user you are now trying to run screen as, right?

There are two ways to resolve this:

  • Sign out and properly connect / sign in as the user you wish to use.
  • Run script /dev/null to own the shell (more info over at [Server Fault](http://serverfault.com/questions/116775/...

Show the character set and the collation of your MySQL tables

To show the collation of your tables you have to login to the MySQL console and execute SHOW TABLE STATUS FROM database;

mysql> SHOW TABLE STATUS FROM test;
+-------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| Name        | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_leng...

Show and change MySQL default character set

To show the MySQL default character set you have to login to the MySQL console and execute SHOW VARIABLES LIKE 'char%';

mysql> SHOW VARIABLES LIKE  'char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | latin1                     |
| chara...

Creating a patch in git and how to apply patches

You can convert git commits into patch files. Those can be used to apply to a different repository [1] or by someone else (e.g. sent when sent to them via e-mail).

Creating a patch in git


  1. Make your changes and commit them.
  2. Run git format-patch COMMIT_REFERENCE to convert all commits since the referenced commit (not including it) into patch files.

For example, let's say you prepared 2 commits. Run:

git format-patch HEAD~~ 

This will create 2 files, one for each commit since HEAD~~, like these:

00...

Stub a request's IP address in a Cucumber scenario

The solution in this card is based on a stack overflow post by Leventix.

If you need to make request come from a fixed IP address for the duration of a Cucumber scenario, the code below lets you write this:

Given my IP address is 188.174.117.205

Rails 3

Given /^my IP address is "(.*?)"$/ do |ip|
  ActionDispatch::Request.any_instance.stub(:remote_ip).and_return(ip)
end

Rails 2
-----...

Ruby 2.0 Implementation Work Begins: What is Ruby 2.0 and What’s New?

While 2.0 will include a number of syntax changes, new features and general improvements, mentioned below, it is anticipated to remain backward compatible with code written for 1.9.3 and Matz has stated that the changes are less significant than those made in the 1.8 to 1.9 jump.

Run multiple local webricks at the same time using different ports

If you specify different ports, you can run multiple local webricks with rails server --port=300X at the same time.

Using :dependent => :destroy – Issues with 'code arrangement' or 'cached objects'

First keep in mind that :dependent => :destroy hooks into before_destroy.
So when you use other before_destroy callbacks the sequential arrangement of your code may be important.

For example:

class Container < ActiveRecord::Base
  before_destroy :a_callback
  has_many :items, :dependent => :destroy
end

results in

container.destroy
# => a_callback
# => container.items.destroy_all

but

class Container < ActiveRecord::Base
  has_many :items, :dependent => :destroy
  before_...

Fix multiple CKEditor instances using jQuery adapter - fixed since 4.2

Using the jQuery adapter breaks the built-in save function of CKEditor.

Phenomenon: The page is submitted correctly, but the original values of the form fields were posted instead of what was typed in the editors.

Work around: Basicly instead of initiating the editor using the above example I ended up using the following:

$( 'textarea.editor').each( function() {

    CKEDITOR.replace( $(this).attr('id') );

});

Note: This assumes that each field using the editor has its own unique ID.

Parse & sort unique hits in logfiles

If you want to know the exact hits on your website (or whatever logfile you want) for a specific date without duplicates, here's how.
"Unique" means you don't want to count hits to an URL originating from the same IP twice.

You can use the attached script to do so:

# ./log_parser.rb 2011-10-04

27 hits on /rss.xml
36 hits on /stylesheets/fonts/slkscr-webfont.woff
37 hits on /stylesheets/fonts/slkscrb-webfont.woff
37 hits on /images/bullet.png
38 hits on /images/download.png
38 hits on /images/play.png
39...

How to install a debian/ubuntu package without dependencies

Please note that you can break your system with this! This is not recommended.

Sometimes the package repository has errors. If you can't install a package because you can't install the dependencies with the package manager you can try this:

(You have to download the deb first e.g. from here or here)

dpkg -x bad-package.deb common
dpkg --control bad-package.deb

Then you have to edit DEBIAN/control with an editor. You can delete the broken dependencies t...

Paste X selections into your terminal with your keyboard

The buffer where text is put into when you double-click on text within X is called X selection.

You can paste this buffer (at least into a terminal window) by pressing Shift + Insert.