blog

simple auto complete text field

Posted in blog on Mar 26. Tags: , , , ,

I have been working on understanding the text_field_with_auto_complete functionality in Ruby on Rails for a project I am currently working on. There seems to be two written tutorials on the subject and they were a little confusing. Here is my attempt to be more specific.

Goal:

While editing an address record I want to show the user a short list of available suburbs that match what they have begun typing into he text box; and allow them to pick the one they want to use. When they save the record, I only want to store the ID of the suburb they typed in, not the actual text.

Solution:

Install the auto_complete gem:

ruby script/plugin install git://github.com/rails/auto_complete.git

This didn’t work on the windows system I was using, so I ended up having to grab the ZIP file and copy the contents to the ‘vendor/plugins/auto_complete’ folder the above command created.

First of all add to the site.rb model so we can refer to the suburb_name as if it was a field in the sites table:

def <span style="color: #ff00ff;">suburb_name</span>
  <span style="color: #00ccff;">suburb.name</span> if <span style="color: #ff6600;">suburb_id</span>
end

def <span style="color: #ff00ff;">suburb_name</span>=(value)
  self.<span style="color: #ff6600;">suburb_id</span> = Suburb.find_by_name(<span style="color: #ff0000;">value.split(',')[0]</span>).<span style="color: #000000;">id</span> unless value.blank?
end

Update the sites/edit.html.erb view to do the auto_complete markup:

<p>
  <%= f.label :<span style="color: #ff6600;">suburb </span>%><br />
  <%= text_field_with_auto_complete <span style="color: #993366;">:site</span>, :<span style="color: #ff00ff;">suburb_name</span>, {}, :skip_style => false %>
</p>

Add an action to the sites_controller.rb so the auto_complete call(s) can get the short list:

def auto_complete_for_<span style="color: #cc99ff;">site</span>_<span style="color: #ff00ff;">suburb_name</span>()  
  <span style="color: #0000ff;">@suburbs</span> = Suburb.find(:all , :conditions=> "<span style="color: #33cccc;">name </span>like '%"+params[<span style="color: #993366;"><span style="color: #000000;">:</span><span style="color: #cc99ff;">site</span></span>][:<span style="color: #ff00ff;">suburb_name</span>].upcase+"%' and we_deliver_to=1")  
  render :partial => '<span style="color: #99cc00;">auto_complete_</span><span style="color: #ff00ff;">suburb_name</span>'
end

Finally, create the _auto_complete_suburb_name.html.erb partial in the views/sites folder, the smallest form of which is like this

<ul><% for suburb in <span style="color: #0000ff;">@suburbs</span> do %><li><%=h <span style="color: #00ccff;">suburb.name</span> %><span style="color: #ff0000;">, </span><%=h suburb.postcode %></li><% end %></ul>

Important stuff you should know

  • I have added highlighting to illustrate the important connections between names as you move between view/model/controller. If you change one, you need to make the same change in every other occurence.
  • The suburbs table has no duplicates across the [name] field and no commas in any name value.
  • The COMMA in the partial is vitally important, I use it to tell the difference between the name value and the rest of the information that I don’t care about (the postcode). Use your own unique seperator if you have comma’s in your data.
  • No spaces in the _auto_complete_suburb_name partial, if you format it nicely your selected result will have lots of extra spaces (you’ll see what I mean)
  • There is no error checking in this. If the user types in a junk suburb the current code will not handle it gracefully.

Facebook fanfare

Posted in blog on Feb 15. Tags: , , , , ,

Facebook. What’s a company to do? It’s not a person, doesn’t have friends and rarely goes to parties.

  • Can your company have a Facebook page? Yes.
  • Can your company have friends? Not really, Companies can have fans (People who like it).
  • Can your company sell products on Facebook? No.

So what’s the point?

Facebook is tricky. You have to look at your company a little differently. You have to look at it from the point of view of the people who are happy (really happy) with your product or service. You have to convince them there is some social benefit to allowing you to send them messages directly. You can’t sell product on Facebook, you give away popularity.

For example: Let’s say company FRS makes shoes. Fancy shoes. Fancy RED Shoes. The kind of shoes that stand out; shoes that people buy so that when other people look at them they go “WOW! Look at those shoes”. Facebook loves FRS. Facebook is designed for FRS to post endless successions of images of beautiful red shoes that it’s fans can say “I want these” or “These are Soooooooo beautiful”. Get the idea?

But your company isn’t like FRS, your company sells lunches in the CBD. What can you do? Post the afternoon specials before 11:30 every morning. Tell your fans what’s for lunch. Make them want it. MAKE THEM WANT IT!

Facebook is ALL about generating comments. Positive comments, obviously, are more delightful than negative ones, but negative comments can be good too. Answer the negative comments. Tell your fans what you are going to do about them. Improve your company. Publicly.

Prove the company to the fans >> get more fans >> generate more interest >> get more fans >> make more happy customers >> get more fans >> build your fan base >> Get. More. Fans.

If your company is in the business of making people happy (with what they bought from you) then you can benefit from a Facebook page. If that’s not true (and you need to be honest here) then it’s probably a good idea to check Facebook for http://www.facebook.com/search/?q=telstra&init=quick” target=”_blank”>hate http://www.facebook.com/search/?q=transperth&init=quick” target=”_blank”>pages with your name on them. Do something about that. It’s free market research with the customers name on it.

Better business through Report Server

Posted in blog on Dec 22. Tags: , ,

I have been having a fantasic time creating reports for Microsoft’s Report Server. It’s actually been very usefull for the company to be able to have PDF versions of various regular reports being automatically emailed to the people who are supposed to be reading them.

The upshot of this automation is that the reports that haven’t been well thought out are quickly brought in to focus and revised by the people who use them. The number of reports that are being sent out has dropped and the information contained in the remainder are salient and accurate.

This translates into fewer people reading shorter reports which allows more time for smart decisions to be made within the company.

Process evolution

Posted in blog on Dec 05. Tags: , ,

With every new release of an upgrade/update comes a new opportunity to re-examine the processes that lead to it’s development. As the software developer I can only refined the automatic processes that lie between the human interactions. I cannot click the button that starts the next step, I can only perform what comes after the button click – up to when another button needs to be clicked.

When the new release is made, a business should also be thinking about whether or not that button needs to be clicked in the same way, or for the same reasons.

This way the business processes are advancing effectively.

Express yourself

Posted in blog on Nov 18. Tags: , , , ,

I’ve just spent two hours talking with my developer about his understanding of The System for a new project. It was an excellent learning experience. I was given a lesson in technical writing for someone else. Where I thought I was being detailed, I was confusing the issues.

Now I have a better understanding of what is a good descriptive method for him. With this information I’ll be updating the existing requirements document, for my internal use. It’s an iterative process that will never end, but well worth the trip.

What’s programming like?

Posted in blog on Nov 04. Tags: ,

I came across this http://www.independentdeveloper.com/archive/2009/03/17/programming-is-like-a-dream” target=”_blank”>Independant Programmer article (via http://weblogs.sqlteam.com/jeffs/Default.aspx” target=”_self”>Jeff’s SQL Server Blog) which tries to explain what it is like In The Zone for a programmer.

It even neatly describes an interrupt routine (boom boom) for non-programmer types, like your boss, to use when they need to get in the way. Possibly worth forwarding on to the higher ups who are prepared to take on a little personal development (or HR management).

Gooey G.U.I.

Posted in blog on Oct 15. Tags: , ,

I found an interesting concept for “The Next User Interface”, over at http://www.techcrunch.com/2009/10/13/10gui-one-very-slick-desktop-multi-touch-concept-video/” target=”_blank”>TechCrunch, called http://10gui.com/video/” target=”_blank”>CON10UUM.

What I like about it is that from start to finish, they have sculpted a new method of interaction with our machines. It’s a pity that we are so attached to our keyboards, but for the moment doing without them is probably too big of a change for anyone to take too seriously. While we continue to depend on the written word for most of our computer based business interactions we can’t do away with them yet, although voice recognition is the next logical replacement it’s not quite there yet (apparantly).

CON10UUM is certainly food for thought and there are products out there that already perform similar tasks (http://www.apple.com/macbookpro/features.html#trackpad” target=”_blank”>Mac touchpads, http://www.wacom.com/bamboo/bamboo_touch.php” target=”_blank”>Wacom Bamboo Touch) but they really only add a thin set of commands to the existing U.I.s that we already use.

Involve the client

Posted in blog on Oct 13. Tags: , , ,

I was talking to one of my developers a few days ago, about how I like to demonstrate completion of The Design Phase.

Having a face to face discussion about the project’s design is a good way to start, but having an interactive presentation (model) to help them “feel” their product is orders of magnitude better. When it’s interactive the client is engaged with the product early and is able to experience their product prior to development. This way I can quickly and simply prove that I was listening when they were telling me what they wanted (during the Requirements Phase).

And with a model of version (n) in front of them, the client can begin to think clearly about version (n+1).

Because everyone in this industry knows; A requirements document is never a perfect description of what the client wants, it’s the best version this time around.

Business Cards

Posted in blog on Oct 06. Tags:

I was sent an invitation to trial a new product about a week ago: miniture plastic business cards and I thought I would just quickly share my excitement about it.

In terms of a user interface, the website was beautiful. So simple. So elegant. Such a great User Experience. Have a look yourself – http://www.squizcards.com” target=”_blank”>www.squizcards.com

The cards are actually really great too, but that’s bye-the-bye for me. What got me in and kept me fascinated, was the ability to quickly customise someone elses designs and make it my own. Hats off to the designers, excellent work!

Documenting for Outsourced Development

Posted in blog on Sep 21. Tags: , , ,

I love the idea of outsourcing development. It forces you to evolve your business processes and it’s entirely in line with the code re-use paradigm found in programming:

[Try to] write re-useable code.

When you are outsourcing your development (especially when you go off-shore) this becomes:

[Try to] write re-useable documentation.

You can no longer have a casual conversation at the coffee machine* with Fred the Coder about what it was you actually meant when you said, “The system needs to save the booking”. The document must stand by itself, it needs to be portable, it needs to be re-usable.

The document creator has to provide a Rosetta Stone for the project: A record of the non-technical description provided by the client and a cohesive translation into the technical description for the development team. When questions are asked by either group, those answers should be noted and incorporated into the (next) version of the document. Even after sigining off on a final version.

If you can provide the perfect Rosetta Stone, then you will only have minimal interaction with the development team which reduces the management overheads. Badly written documentation results in far too much live translation and that leads to missed dead-lines and budget blow-outs.

*or PS3 or Kitchenette etc.