Who Needs Actions

Ruby on Rails, Javascript and Friends.

Simple Google Maps Example With Coffeescript and jQuery

I’m working with the latest version of the Google Maps API (V3) and the demo in the developers guide guide screams out for a dose of Coffeescript and jQuery. The three Javascript requirements here are:

  1. Include the Google Maps API
  2. Define the initialize() function
  3. Call the initialize() function onload (one the page has finished loading)

Here’s a before and after for the Coffeescript/jQuery makeover:

Pow Server for Rails Applications

Many Rails developers have many apps and think nothing of switching between them as necessary with the trusty rails server command. Each morning cosists of the familiar routine: open terminal, start auto-running tests, and boot the dev server. The Pow server makes this final step unecessary.

Pow is a super simple to set-up solution for serving Rails apps on your dev machine. By simple, we’re talking a single command, and by Rails, we’re talking Rack apps which extends to any Rack solution you muster up. The Pow Screencast will get you up to speed in no time. Watch the video, symlink your Rack apps and hit {folder}.dev in your browser, where {folder} is the name of the newly created symlink that should be within ~/.pow/ by default.

Tip: As mentioned above, by default Pow is set up to use .dev, which Google Chrome doesn’t recognise and will ‘helpfully’ treat it as a search query. The solution here? End the call with a forward slash / e.g hamster.dev/. Finally, a solution for using debugger with a Rails app served through Pow can be found here.

Ruby Koans Scoring Project

Ruby Koans has sat on my todo list for almost as long as I’ve been a Ruby developer and this week I’ve finally taken it upon myself to tackle it. Expectedly, there’s little new to be expected for anyone who has served their time as a developer, but there’s plenty of helpful tricks and Ruby intricacies to be reminded of. I’ve enjoyed looking at how others have tackled the problem, so assuming you’ve already given the question a good attempt, here’s my solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def score(dice)
  score = 0

  1.upto(6).each do |num|
    amount = dice.count(num)
    if amount >= 3
      score += num == 1 ? 1000 : num * 100
      amount -= 3
    end

    score += 100 * amount if num == 1
    score += 50 * amount if num == 5
  end

  score
end

Settle for Excellence

Today was spent away from my desk, but I was reminded of a great quote by George S. Patton, an American officer during World War 2. Now, I can’t vouch for his programming chops but the quote very much applies to web projects:

“A good plan violently executed today is better than the perfect plan tomorrow.”

Start now.

Haml Helpers Succeed and Preceed

With Haml, placing a full-stop or comma immediately after a link, for example, can be confusing. Sure, we have the whitespace removal functionaliy removal that gives us the ability to place < or > to remove the space before before and after a tag. The issue is, it’s ugly, unclear and I’m come across instances where it simply doesn’t do as advertised.

The haml helpers page helpfully offers the succeed and preceed helpers that can be used as follows:

1
2
3
= succeed '.' do
  %a{ href: 'example.com' } Example
# => <a href="example.com">Example</a>.

If it isn’t clear, the succeed helper takes a block which it’ll evaluate and place the passed string directly after, without white space. In this case, a full-stop is placed directly after the generated link. preceed works the same, but places the given string before the output.

Writing out my own example was motivated purely by my strong preference for the Ruby 1.9 hash syntax. As far as programming tidbits go, this is a good candidate. Small and tasty. Till Monday!

Geolocation for Amazon Link Localising

This post starts a 30 days experiment. I’m not alone in starting a blog with every intention of keeping consistent, only to find weeks fly past without so much as a grunt. So for 30 days, this blog takes on a new format: each day I will end my ‘working day’ with something I learnt that day. However small, however insignificant, someone somewhere will stumble where I fell or ponder thoughts I’ve processed.

A purpose of today’s post is to make you aware of FreeGeoIP, a solution for those wanting to look up a visitors country by IP. In my case, I’ve started a site where I hope to teach portuguese and wanted a way to link to a mentioned book to the appropriate Amazon website.

Easy HTML Table Pagination and Sorting With DataTables

DataTables is a powerful jQuery plugin that easily turns a standard HTML table into one that is sortable, paginatable, searchable and highly customisable. Having used the plugin on multiple occasions over the past few months, I deem it a huge time saver and if it’s interactive tables you’re after – I wouldn’t look elsewhere. The basic example usage makes its immediate power clear and there’s a wealth of options, an API and plenty of examples to satisfy most cases. Here’s a few tips on how to handle a few of the requirements I commonly had. I love and use CoffeeScript but for your convenience, the examples below make use of the compiled Javascript, simplified in places.

Solution for Osx Lions Wi-fi Problems

My story is like many others out there: Having purchased a new Mac in the last few weeks, arriving pre-installed with Lion, I had a few days of bliss before the problems began. Failing to connect to my wireless router, incredibly slow speeds when it did and unresponsive the majority of the time. Steve Jobs had ripped my heart out using nothing but a spoon.

I tried clearing various ram caches, recreating the wireless connection in network preferences, setting custom DNS, manual IP address, rebooting the router, performing dance rituals to the wireless dities etc and I was beginning to lose hope. Mention of an upcoming 10.7.1 update had me hopeful, but upon installation I had a few minutes of joy before the problems returned with all it’s troublesome tendencies. So what did work for me? Changing my wireless routers channel, specifically to channel 4 (avoid anything over 11 for the BT HomeHub router).

Nginx Error: No Such File to Load – Rack/commonlogger

Despite a lack of experience with server administration, I jumped in the deep end this week by grabbing a VPS from slicehost. With it, you start from scratch, with not even the firewall iptables setup to allow port 80. You’re given root access to ssh and sent on your way, which is one level above being given a screw driver and then directed to a pile of hardware components. But with frustration and confusion comes learning and knowledge… got me feeling pretty powerful over here.

The aim was to set up a Sinatra app, running with Nginx and Passenger. The installation of all was easy enough, up till the final step of pointing Nginx to my Sinatra app and seeing the following error: Exception LoadError in application (no such file to load -- rack/commonlogger)