Ajax Autocompletion With Rails

First, I’d like to apologize for the long delay in posting to my blog. My wife and I had our second child at the end of September and things have been hectic since then. I was playing around with the ajax autocompletion code from script.aculo.us but couldn’t get it working in my own code. It turns out that in addition to prototype.js in your layout RHTML, you also need to include effects.js and controls.js, so the code looks as follows:

  <%= javascript_include_tag :defaults %>

In the controller, I wrote my own autocomplete_for_XXX method since I wanted to search several different fields in my database. Note that there is probably a much better way to achieve the following without making three different calls to find and without making calls using LIKE %foo% …

  def auto_complete_for_resource_name
@search_term = params[:resource][:name].strip.downcase
@resources = Resource.find(:all,
:conditions => [ 'LOWER(name) LIKE ?',
'%' + @search_term + '%' ],
:order => 'name ASC',
:limit => 10)
@resources.concat(Location.find(:all,
:conditions => [ 'LOWER(name) LIKE ?',
'%' + @search_term + '%' ],
:order => 'name ASC',
:limit => 10))
@resources.concat(EqType.find(:all,
:conditions => [ 'LOWER(name) LIKE ?',
'%' + @search_term + '%' ],
:order => 'name ASC',
:limit => 10))
@resources.uniq!
render :partial => 'resources'
end

You’ll need a partial _resources.html such as this:

<ul class="resources">
<% for resource in @resources do -%>
<li class="resource">
<div class="name"><%=h resource.name %></div>
</li>
<% end -%>
</ul>

Finally, in the RHTML where you want the autocompletion to appear you need to add a text_field_with_autocomplete, but since I’m using this in a search field (similar to Google’s usage), I needed to place it within a form:

<%= start_form_tag :action => 'search' %>
<label for="reservation_resource">Search</label>
<%= text_field_with_auto_complete :resource, :name %>
<%= submit_tag 'Search' %>
<%= end_form_tag %>

That’s all there is to it. Hope that helps — all of the other documentation I’ve seen only has you include prototype.js and that’s where I ran into difficulty.

Ruby on Rails + Yahoo! Widgets

After attending OSCON session on Yahoo! Widgets by Jeffrey McManus, I decided I would try to write a widget which connected to Ruby on Rails. First, the Yahoo! Widget (aka Konfabulator) app is located here and the developer documentation is located here. Second, to save time, I simply modified the Depot code from the excellent Agile Web Development with Rails (I hope Dave and David don’t mind). You will need to first download the code for the book and extract the depot_final project (and setup your database, etc). Then you can download my code here and clobber admin_controller.rb with my version, then add shipping.rxml in the views/admin subdirectory. Finally, the JavaScript source code for the widget is located in SimpleDemo.widget/Contents/SimpleDemo.kon file (you might need to rename SimpleDemo.widget to SimpleDemo.widget.zip to unzip it and view the source). It should be pretty easy to understand as I’ve commented it well.

OSCON Observations

Some observations from OSCON. The short form is that Ruby,Ruby on Rails and Ubuntu were the biggest things around at this year’s OSCON, closely followed by Ajax. My more detailed observations are continued below (except for Ubuntu, since I can’t really add anything — I use it, I like it, but if another Linux distribution comes along that I like better I’ll easily switch).While Java and Perl still occupy a significant mind-share, it appears that share is receding. Perhaps we are at an inflection point, where the (open source) development community is moving towards Ruby (and Python). To an Java/Perl programmers reading this, I suggest you learn one or both of these languages. Due to work, I had to learn Python first and Ruby in my spare time — the Pythonistas will probably lynch me, but if I had a choice I would’ve skipped Python in favor of Ruby. It seems that there is a bit of (albeit friendly) competition between the Rubyists and Pythonistas — though instead of copying killer apps (*cough* Subway *cough*) the two groups should work together to help those poor souls stuck in Java, C/C++ and Perl move into the 21st century (or back to the middle of the 20th century, since Lisp had all of these goodies first — I couldn’t resist ;–)). Developing web-apps is hugely popular as evidenced by all of the love going Ruby on Rails’ way as well as the interest surrounding Ajax. Alex Russell, of Dojo Toolkit fame, gave a couple of different tutorials/sessions on Ajax/JavaScript and both were quite full (the tutorial was sold out). Now Alex is a great speaker and seems like a nice guy, but I don’t think all those people were there just for him — the recent coining of the XMLHTTP “technology” Ajax (which has been around in IE for quite a while) seems to have brought it to the forefront (that and the folks using our web-apps are more savvy today and won’t put up with full page refreshes and stupid web UIs which make them jump through hoops). I’m just getting up to speed on this stuff myself since the majority of programming work has been with “traditional” client-apps. Since most open source developers have the luxury of choosing their programming language when starting a project, they will usually choose the one they are most productive in. Companies need to anticipate this change so they can hire the best developers. Developers who do not take the initiative may find themselves in danger of losing their jobs, as unlike the transitions from FORTRAN to C or C++ to Java, many businesses have realizes they can find just as well educated employees in countries with much lower costs of labor. Google still gets quite a bit of “love” from the open source community. I’m a bit surprised at this — not to say that Google is bad (or “evil”) in any way — just that Google is perceived as the internet leader. What about Yahoo!? Yes, everyone here still loves Flickr as well (and it is somewhat deserved), but I think the euphoria around Google has created an RDF (Apple folks will know what I mean). The Yahoo! Developer network has released a bunch of new APIs/tools within the last couple of weeks which are amazing. Because of this, I think that either Google’s stock is currently overvalued or Yahoo! is undervalued — it is probably the former, but since uncovered shorts are risky (and Google stock is still so volatile), I’d recommend buying Yahoo! instead. Disclaimer, I don’t own any Yahoo! or Google stock, yet.

OSCON Friday Keynotes

The OSCON Friday Keynotes are over and here are my notes. To save folks some trouble (in case none of these people interest them), I’ve listed the speakers:

  • Conference Announcements – Nat Torkington, O’Reilly Media, Inc.
  • Linux – In Search of the Desktop – Asa Dotzler, Mozilla Foundation
  • Open Source Biology – Drew Endy, MIT Biological Engineering
  • Open Source Licensing Issues – Tony Gaughan, Computer Associates
  • On Evil – Danny O’Brien, Need To Know
  • Howtoons – Saul Griffith, Squid Labs

And the afternoon closing session was keynoted by Miguel de Icaza. The first speaker was Asa Dotzler from the Mozilla Foundation regarding his controversial post that Linux is not ready for the desktop and what the Linux developer community needs to do to get it there. In general, I agree with Asa’s points regarding Linux — but I think it is largely irrelevant (or will be) now that all of the interesting applications are moving on to the web — pretty soon folks won’t care what OS their machines are running as long as the browser works (perhaps Linux on the desktop will eventually win — but as a cut down, minimal OS that runs on the cheapest commodity hardware and simply boots quickly into a full-screen browser). Next was Drew Endy and Assoc. Prof at MIT Biological Engineering. His presentation on DNA as the code for writing/building biological organisms was quite good. It seems that they are also dealing with difficult patent issues in his field as well because as companies discover which strings of DNA-code do what, they immediately patent the work. He gave the example of students who made an organism which essentially took photographs (the billions of bacteria would rearrange themselves to reproduce the image shone onto them) — this was only possible because the building blocks necessary for this project were freely given to them by the various universities which own the patents on them. Following Drew was Tony Gaughan from Computer Associates, at first I thought this was going to be another marketing keynote, this time for CA’s Ingress open source database. While Tony did mention Ingress quite a bit he also went over the open source licenses which have been written in the last few years and how this “explosion” of licenses is not sustainable, fractures the open source community and in general makes this very difficult for commercial interests who wish to use and contribute back to open source projects. I completely agree with this and think that the OSI needs to do something about it. If they can boil down the essence of each of the existing licenses and build up a sort-of template library of licenses where-by developers could simply use one or more templates to licenses their software. Danny O’Brien, the editor of Need To Know (and now apparently affiliated with the EFF) gave a hilarous talk on “evil”. I can’t really do a good job of conveying it since you really had to be there. Finally, what I thought was the most interesting talk came from Saul Griffith of Squid Labs. He spoke about the HowToons comics which his company is developing (there are plans for a book as well) to get kids interested in science and engineering (and let them have a lot of fun while doing it) as well as the iFabricate document and collaboration system for helping people share projects with one another in a standardized way. He then showed videos of some of the projects they have implemented with kids. For afternoon closing session, Miguel de Icaza (of Novell) spoke about Linux on the desktop and the work at Novell to completely replace MS Office with OpenOffice (done) and replace MS Windows with Linux (50% done). He spoke about the portable usability testing lab they have to watch how Windows users deal with the migration to Linux (this information is passed back to the Linux/GNOME developers). He then gave a demo of the forth-coming OpenSuSe Linux distribution with a completely re-written UI layer that is “quartz-like” in that it renders to PDF using OpenGL (hardware or software emulation). The demo was really neat, he could change desktops by simply dragging the desktop around (as it was really a cube), he show how quickly moving windows made them wobble (like Jello), he showed an Expose-like feature which unstacked the windows so you could find what you’re looking for and he showed a video playing that was moved halfway between desktops — so that when he tried to switch to the next desktop the movie window bent around the corner of the cube (and it still played fine). ISOs for this should be available sometime next week, he said. Overall, it was an interesting tcchnology demo — not much new was said. Since I’ve got to go to the airport, I’ll post my OSCON observations and thoughts on the trends of software development over the weekend. I had a great time at OSCON and hope to come back next year.

OSCON the Do-It-Yourself Mindset

Phillip Torrone, the Associate Editor of Make Magazine, demoed some great Do-It-Yourself items and talked a bit about the recent resurge in the DIY movement. He showed how to replace the Japanese firmware on the Sony Libre with an English version — after that you can download tons of eBooks. He also passed it around and the resolution is amazing, even in direct light. Following that he showed how you can use homebrew games as well as a web browser (from the crappy Wipeout game) on a Sony PSP (as long as you haven’t upgraded the firmware to 2.0). After that he showed us his mouse bot (as seen in Make magazine) which gets him into trouble with the TSA each time he travels. Then he showed Linux on the iPod — it’s ability to record up to 96Khz quality audio and play back video. He also showed us what he did with this iPod box (made it into a cardboard/foam robot for kids). The penultimate demo was a powerful green laser which cooks/burns anything red (he demonstrated by popping red balloons). Finally, he demo’ed an old rotary phone which he hooked up to a cell phone (then folks made several phone calls to/from his franken-rotary phone). I highly recommend subscribing to Make — you won’t be disappointed. Update from PDX’s Free WiFi: Phillip Torrone posted video from his “mini-MAKER” fair on Wednesday night at OSCON and I’m in the video! Well, part of me is, I’m standing to the right of (left of, from the camera’s perspective) and slightly behind the laser so you can see my khaki pants and red shirt.