Basic git commands

This is for people recovering from Subversion.

Get an existing from the server for the first time

git clone git@example.com:repositoryname

See what's changed

git status

Check in locally

git commit -m "good description"

Push local commits to the server

git push

Get and merge updates from the server

git pull

Stage a file for the next local commit

git add file

Stage all files for the next local commit

git add .

Create a new local branch and check it out

git checkout -b branchname

...

Test if a checkbox is checked in jQuery

jqueryElement.is(':checked')

Use the ALT key in a VirtualBox Windows VM

In Ubuntu your ALT key is locked when you're working in VirtualBox. There are two workarounds for this:

  • Press the Windows key in addition to the ALT key
  • Run the attached registry file and restart Windows. Your left Windows key is now the ALT key.
  • If you'd like to map keys differently, here are further instructions

How to fix "Too many authentic authentication failures" with SSH and/or Capistrano

You are getting when connecting via SSH or deploying with Capistrano (which uses SSH):

Too many authentication failures for username

This is caused by having too many SSH keys added to your keyring or ssh-agent. Your ssh-agent will throw all keys against a server until one matches. Most servers will deny access after 5 attempts.

This issue might come and go as the order of the active SSH keys in your ssh-agent changes.

Quick fix

Have less keys. Up to 5 keys are fine when the SSHD you're connecting to is using the default ...

Why your all.js is empty on staging or production

When you include a non-existing Javascript file, you probably won't notice it during development. But with caching active (on production or staging) Rails will write an empty all.js file without complaining.

Copy a Paperclip attachment to another record

Just assign the existing attachment to another record:

new_photo = Photo.new
new_photo.image = old_photo.image

Paperclip will duplicate the file when saving.

To use this in forms, pimp your attachment container like this:

class Photo < ActiveRecord::Base
  has_attached_file :image
  
  attr_accessor :copy_of

  def image_url
    if copy_of
      copy_of.url
    else
      image.url
    end
  end
end

And in the controller do:

new_photo = Photo.new(:copy_of => old_photo)

Force absolute URLs in views throughout a response

This is more tricky than it should be because url_for, asset_path, etc. all rely on different mechanisms.

Anyway, you can use the attached trait like this:

class ExampleController < ApplicationController
  does 'host_enforcement', :for => 'some_action'
end

Short explanation:

  • asset_host is used for links to stylesheets and javascripts
  • asset_host belongs to ActionController::Base -- changes are persistent and will not be reset after a request
  • rewrite_options is used by the ..._path methods in the views
    ^...

Unobtrusive JavaScript and AJAX

Attached (see below) is some code to allow using unobtrusive JavaScript on pages fetched with an AJAX call.

After you included it, you now do not use the usual

$(function() { 
  $('.some_tag').activateStuff(); 
});

any more, but instead you write

$.unobtrusive(function() {
  $(this).find('.some_tag').activateStuff();
});

that is

  • $.unobtrusive() instead of $()
  • don't do stuff to the whole page, but just to elements nested below $(this)

Normal pages work as before (your $.unobtrusive functions are ca...

Highlighted prompt for production shells

Insert into ~/.bashrc and reload afterwards: source ~/.bashrc

PS1='${debian_chroot:+($debian_chroot)}\[\033[41;33m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '