jQuery geographical data autocompleter in Rails

In Wadomo, we enforce writting reusable code when possible. This means that we can use it in our future projects, but also that all the developers out there can use it and improve it at the same time. Win-win situation that has been the base for the open source community since the early days.

A common problem that arises within web projects is how to save geographical data about our users. We always like to know where users are located, so we can target them with more localized information and recommendations.

Our goal was to easily add this information in our views, and to be able to have an autocomplete field with all the cities in the world with just one html tag, simple and clean.

Long time ago, we used to create big, annoying and unmaintainable relational tables in our databases, trying to solve this problem. Now, we can use some of the online services that companies like Google or Yahoo offers. After testing some of these services, we decided to use Yahoo Geo and we are very pleased about the obtained results.

To make our life easier, we have developed a Ruby on Rails plugin, and of course you can use it too! This plugin will include jQuery in your project (if you haven’t done it yet) so please make sure that you are not using Prototype, because it could create conflicts with your code.

The first thing that you have to do is register yourself in Yahoo Geo for an API_KEY. After that, you’re ready to install our plugin.

Using a console, go to your rails app dir and type:

script/plugin install git://github.com/wadomo/geo_yahoo.git

Now, check if  you have included the default javascript files in your layout. If don’t, include this line in your views layaout:

<%= javascript_include_tag :defaults %>

The last thing, is add your API_KEY in your config/environment.rb file. Be sure that you include this line just before the last “end” of the file.

ENV['YAHOO_API'] = “yourapikeyhere”

That’s it! You’ve got it!