Ahhh Google

I just got my new iMac. Quite a nice pice of kit. Even if I couldn't cope with the mouse and the keyboard. But I tried to add my Gmail account to the mail client and the iCat. But it keept failing with "Invalid Credentials (Failure)" After some googleing and looking around someone suggested you should unlock a captcha. https://www.google.com/accounts/UnlockCaptcha?
And now everything works fine. WTF is that? Anyway now back to searching where my iCat window has gone

Ruby on Rails with text_field_with_auto_complete

I spent some time trying to figure out how to get a little text field with auto complete in Rails. Now I wanted many fields to have the same suggestions in one form. So here are some code samples. For some information. I have a model that is called Locations, basically this is just a collection of names of cities a person should be able to travel to.
In my controller I need

# The first value is the model the second the name of the field

auto_complete_for :location, :name

# As of rails version > 2.0 forgery protection is activated by default. As the
# AJAX stuff won't work otherwise you have to disable it for one method

protect_from_forgery :except => [:auto_complete_for_location_name]

# This is the method called my the AJAX obj
def auto_complete_for_location_name
leg= params[:location].keys[0] # get index as its always only one at a time

auto_complete_responder_for_name params[:location][leg][:name]

# This does the real magic. It queries the database and then renders a
# partial 'names' with the list of values to display. With some sql knowledge

# you should be able to understand the query
def auto_complete_responder_for_name(value)
param= '%' + value.downcase + '%'
find_options= {
:conditions => [ 'LOWER(name) LIKE ?', param ],
:order => 'name ASC',
:limit => 10
@names = Location.find(:all, find_options)
render :partial => 'names'


So that is for the controller. Now we need to create the partial. You can call this what ever you want but for now I called it names. So you need a file named '_names'

<ul class="autocomplete_list">
<% for name in @names -%>
<li class="autocomplete_item"><%= name.name %> </li>
<% end -%>

This is quite simple it get's the array @names we built in the controller and builds an unordered list out of the elements.

Next we have to add a little helper method to the app/helpers/application_helper.rb file. This is basically the wrapper for the 'real' auto_complete_field.

# Methods added to this helper will be available to all templates in the application.

module ApplicationHelper

def my_text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})
tag_name = "#{object}_#{tag_options[:index]}_#{method}"

tag_name = "#{object}_#{method}"

(completion_options[:skip_style] ? "" : auto_complete_stylesheet) +
text_field(object, method, tag_options) +
content_tag("div", "", :id => tag_name + "_auto_complete", :class => "auto_complete") +
auto_complete_field(tag_name, { :url => { :action => "auto_complete_for_#{object}_#{method}" } }.update(completion_options))


So now we should have everything that is needed to create our little textbox. Now in the view you just add
<%= my_text_field_with_auto_complete :location, :name, {:index => '1'} %>
And you increment the index value for every new field you add in that view.

Thank you to Wolfmans Howlings for posting all this.

Hacking tip 2035

In my flat in Geneva I have a very comfortable chair. It is made out of leather and I really enjoy sitting in it. So today I go up and didn't want to dress as it was already 35 degrees in my room. So I sat in my chair and started hacking. 3 hours later I needed to get up for something to drink. Now here is the problem. My skin and the leather decided they wanted to join. I am presuming this is encouraged through the heat. Now I was stuck to the chair. If I tried to get up it started to pull of my skin. Very painful I tell you. So conclusion: I now had a professional back peeling. And a tip for all you with leather chairs never sit on them without at least a t-shirt.

Why Debian is not Ubuntu and vice versa

Reading the arch Linux newsletter (http://www.archlinux.org/static/newsletters/newsletter-2008-Aug-04.html) and talking to some of my friends. I was noticing that many people now see ubuntu as debian, they are now seen as one thing. Many people that complain about ubuntu could just run debian. It has been quite a long time since I had a debian install but I think you can even install it without X. So if you don't want the bloated windows copy, that ubuntu is, but you still like the pakages why not install debian. Ubuntu might be using debian as a base source of packages but please don't say
(As of this writing, I'm still unaware of any bloat-free editions of Ubuntu.)
beacuse that is debian.