Search_spinner

Add a prefix to form field IDs

If you use a form (or form fields) multiple times inside one view, Rails will generate the same id attributes for fields again.

This card presents you with a way to call something like

- form_for @user, :prefix => 'overlay' do |form|
  = form.text_field :email

and get this as HTML:

<input name="user[email]" id="overlay_user_email" (...) />

You can also put a :prefix into a field's options. Note how only the id but not the name changes as we would not want to pass an overlay_user[email] param to the controller. Settin…

External content

Configuring User Agents with Capybara + Selenium Webdriver

A while ago we were working on an application that had an entire version specially created for mobiles, such as the iPhone. This specific application was entirely tested with Capybara, Steak and Selenium Webdriver. Although the test suite wasn’t the fastest one in the world, the web application was very well tested, and to guarantee that we would also be testing the mobile version, we would have to simulate an iPhone user agent accessing the application.

But wait, you might be thinking that we are not able to change browser headers while …

sudo command for Windows

There is no good equivalent for the Unix sudo command in Windows. Below are a few workarounds that are all unsatisfactory to some degree.

Run the command prompt as Administrator

  • Right-click on the Command Prompt icon
  • Select Run as Administrator
  • Do what you need to do
  • Quickly close the terminal

Sudo for Windows

If you are using an English Windows, you can use Sudo for Windows. Unfor…

How to change MySQL's data directory

  1. Have a backup.
  2. sudo service mysql stop
  3. Move (or copy) your mysql directory. If you want /mnt/mysql to be the new directory, do it like this:
    sudo mv /var/lib/mysql /mnt/
  4. sudo vim /etc/mysql/my.cnf
    Change the datadir value to your new path (e.g. /mnt/mysql)
  5. sudo vim /etc/apparmor.d/usr.sbin.mysqld
    Change/clone the lines granting access to /var/lib/mysql to your new path. Otherwise MySQL will not start.
  6. sudo service apparmor reload
  7. sudo service mysql start

Securely remove files on Linux

When you delete a file with rm it's still possible to recover the file or parts of it. Use shred to overwrite the content of a file and delete it afterwards.

shred -u $file

Be aware that this is not sufficient for flash memory, like SSDs or USB pen drives. The write will possibly target another memory area than where the file was located previously. Prefer disk encryption whenever possible.

Erasing directories

To erase complete directory structures, using shred is cumbersome. Use wipe instead:

wipe -...

Dump your database with dumple

This tool is used on our application servers (and called when deploying) but it also works locally.
Just call dumple development from your project directory to dump your database.


This script is part of our geordi gem on github.

Exchange messages between Javascript and Flash

Flash movies (.swf files) can talk with Javascript code embedded in the same HTML page. There are two ways to do this:

  • The preferred way is to use the ExternalInterface class to call Javascript functions from ActionScript, and to bind ActionScript functions to the Flash movie's DOM element so they can be called from Javascript.
  • The deprecated way is to use the global [fscommand](http://www.adobe.com/support/flash/action_scripts/actionscrip…

Paperclip: Image resize options

Paperclip uses the imagemagick resize options like 100x50> , 100x50<, 100x50# etc to resize images. See the link what options are available.

  • Ignore Aspect Ratio ('!')
  • Only Shrink Larger ('>')
  • Only Enlarge Smaller ('<')
  • Fill Given Area ('^')
  • Percentage Resize ('%')
  • Pixel Area Limit ('@')
  • Crop thumbnail centrally and ensure the requested dimensions ('#') (not documented at imagemagick doc)

Ubuntu: Open a new terminal window with a keyboard shortcut

To open a terminal quickly navigate to System → Preferences → Keyboard Shortcuts. There, click the "Run a terminal" row (It should be in the "Desktop" section) and press the keyboard shortcut you like.

If you cannot find such an entry you may need to add it yourself – the command for Ubuntu's default terminal is gnome-terminal.

Be aware that many programs use shortcuts of their own and you would not want to clash with those (like Ctrl+T). Something like Meta+T is something usually not ta…

External content

HTML5 and Web Video: Questions for the Industry from the Community

What are Google’s plans for turning WebM into a genuinely open standard, one that is based on consensus like the rest of W3C’s HTML5 effort? Would Google fully support such an effort? Even the WebM project’s domain is controlled by Google. Google chose to release WebM under the Creative Commons license which would theoretically allow a standards body to use the specification as a basis for a truly open standard. Would Google agree to adopt the specification and changes that would emerge from an open process in a timely and robust manner? Wha…

Decrypt OpenSSL encrypted backup files

We use OpenSSL to encrypt backup files from our production machines. You can use the following command to decrypt those files:

openssl enc -d -salt -aes-256-cbc -in $filename_to_decode -out $decoded_filename

Validate an XML document against an XSD schema with Ruby and Nokogiri

The code below shows a method #validate which uses Nokogiri to validate an XML document against an XSD schema. It returns an array of Nokogiri::XML::SyntaxError objects.

require 'rubygems'
gem 'nokogiri'
require 'nokogiri'

def validate(document_path, schema_path, root_element)
  schema = Nokogiri::XML::Schema(File.read(schema_path))
  document = Nokogiri::XML(File.read(document_path))
  schema.validate(document.xpath("//#{root_element}").to_s)
end

v...

Insert an ndash and other special characters using the Compose key under Linux

Although you can access many symbols using the AltGr key you may be missing some, like the en-dash (–) or em-dash (—). You can use a compose key for them instead.

First, make sure you have a compose key configured.

Configuring a compose key

Ubuntu / Gnome 2
Check System → Preferences → Keyboard → Layouts → Options → Compose key.
If you don't have, I suggest using the "Menu" key which is located between the right [Meta](http://en.wikipedia.org/w…

Quotation marks in Cucumber steps

When you have a Cucumber step like

Then I should see "Did you see those \"quotation marks\" over there?"

you'll run into some trouble. Cucumber won't take your escaped quotation marks.
The solution is to write the step as regex (since there is a step taking a regex):

Then I should see /Did you see those "quotation marks" over there\?/

Keep in mind it’s a regex – escape regex characters like '?'.

Maximum representable value for a Ruby Time object

On 32bit systems, the maximum representable Time is 2038-01-19 03:14:07 in UTC or 2038-01-19 04:14:07 in CET. If you try to instantiate a Time with any later value, Ruby will raise an ArgumentError.

If you need to represent later time values, use the DateTime class. This is also what Rails does when it loads a record from the database that has a DATETIME value which Time cannot represent. Note that there are some [subtle differences](http://stackoverflow.com/quest…

Encrypt files using OpenSSL

Using OpenSSL it's very easy to seriously encrypt files. Use the script below. Input / Output are self explanatory. Put a long passphrase into PASSWORD_FILENAME. It is the key to decrypt to file again. Paste this into a console and copy it to wherever you need it.

touch /tmp/openssl_encryption_password && chmod 0700 /tmp/openssl_encryption_password && apg -n1 -m 60 > /tmp/openssl_encryption_password

Remember to at least delete the input file afterwards. Better use [shred to wipe files on Linux](http://makandra.com/notes/1013-securely-r…

Continously run command under bash

Sometimes you want to run a command forever, e.g. to compile a haml to html file on the console. Use this:

$ while(true) do haml index.haml index.html; sleep 1.5; done
2392 cards