Rails 4 Default Host Change

This has caught me out a couple of times now, so I’m blogging it in the hope it will help me to remember :)

If you’re running Rails, and using Virtual Box to test on other operating systems (such as working on a Mac and testing Internet Explorer) – or even accessing your local development site from other devices to test it – then you might also have run into it.

In Rails 4.2, a change in Rack meant that the server now listens on localhost instead of by default.

That means if you’re trying to access your local development site via your IP address on an internal network, it’s not going to pick it up. The fix is to start the server with -b

rails s -b

Beware, though – the reason for the change is to restrict malicious users from accessing and exploiting the app, so use with caution! If you’re in a coffee shop, at a conference, or even using a shared network in a co-working space, use with care, and use a firewall.

Haml Tips and Tricks

I use Haml for my templates, and I mostly love using it and find it very intuitive. However, there are a few things that seem to trip me up on a regular basis, and I constantly find myself googling for the same Stack Overflow posts … so now I’m doing something I should have done ages ago and just posting them here for easy reference. Hopefully they might even help somebody else.

Don’t escape my HTML

By default, Haml escapes HTML, because in most cases this helps protect from XSS – but sometimes it’s my HTML and I know it’s safe, so I don’t want it escaped – or maybe I really just wanna let the bad guys hack me. For this, use an exclamation mark (!).

!= "I like my html UNESCAPED!!"

Thank you Haml Docs.

Whitespace removal

Ever want to do a link followed by a comma? You know, like that? Only if you do that in Haml:

%a know
, like that?

It comes out with a space: You know , like that? But you can fix it with the greater than operator, >, which removes all whitespace surrounding the tag.

Thank you, Haml Docs and Heikki on Stack Overflow

, like that?

Inline, Indented Javascript

This is almost always a bad idea, but if you find yourself forced to do it, use the javascript filter.

  function myFunction() { 
    // do some stuff

Thank you Haml Docs and bcoughlan on Stack Overflow.

The Haml docs contain a lot of other useful stuff, but these are at least two things that I am tired of looking up!

Let your users direct the tech

I was trying to explain to my co-founder Liz last night the changes that I’d been making to Cake, the system that we use to process enquiries from our website, when she frowned and said, “that sounds really complicated.”

It stopped me in my tracks – technology is supposed to be enabling our company to move faster! She went on, “Trello is so simple in comparison!” She’s right on that – Trello IS simple but it only covers one part of of the process, while Cake is trying to do all the things. But it was time to stop ignoring that niggling feeling that I was losing my direction on what I was building, and revisit the system with the person who actually uses it every day.

So co-founder-2 (Phil) and I sat down today, hooked his laptop up to a large TV screen and started putting orders through on the staging site, all the way from the customer placing them, through to the supplier accepting them, and the customer confirming and paying. I played the part of the customer, and Phil took on the two hats of the supplier and himself, doing his daily job of processing orders for You Chews. It was a bit confusing – would have been better with Liz to act as supplier!

Things started off slowly. I’m using a Kanban system to pull changes through to production, and I’m releasing changes sometimes several times a day. For Phil, that means that the system and processes he uses are changing every day – and even when it seems to me like it’s for the better, sometimes it’s just a PITA. It’s not until you see somebody else using the system that you built that you start to see whether or not it will actually work!

The first order was a bit of a struggle. It became obvious fairly quickly that Cake needs to update the order status automatically, that having that as an extra step makes it significantly more confusing.

With the second order, we discovered some bugs in the steps to email the supplier – luckily they were just config issues, so I fixed them, and we were off again.

The third and fourth orders started to go more smoothly. Phil was following the steps he had noted down for himself, knew that there would be upcoming changes to automate the status updates, and asked loads of questions. He consistently deleted one part of the email that would go to suppliers, so it was really clear that it wasn’t needed. Towards the end of the meeting, he was even getting excited – “This is great! This is great!” – he even said at one point. We discovered a ton of places where small changes like wording could make the process smoother, and I walked away with a much clearer vision of what I need to create to make the system useful for him. Phil even left saying that he’d enjoyed the meeting – we spent about two hours in that room, so believe me that is quite an acheivement!

It reinforced a few things for me. Firstly, and most obviously, test the system with real users. They can be customers or staff – and don’t just do a quick test, let them use it a few times, see if you can provide help and direction and if that makes a difference, and watch what they do. The more they use the system, the more useful feedback they’ll be able to provide you.

Secondly, if you’re working in a fast-paced environment, constantly pushing through changes, it’s a good thing to stop and review the bigger picture every so often. It doesn’t need to be a big, formal process – I spent five minutes sketching a diagram of what I was trying to do, and fifteen minutes talking it through, and was far more confident that I was heading in the right direction.

Lastly: if you are going to do user testing, expect to walk away with a longer to-do list!

Debugging Cucumber tests on Codeship

I’ve been running into a few problems recently where my cucumber tests fail in the build but not on my local machine. It’s often related to the tests running slightly faster than the javascript on the build machine, but it can be really hard to catch.

A couple of things have worked well. First of all, I’ve added capybara-screenshot gem to my cucumber tests – setting it up was as simple as adding it to the gemfile and putting one line in the env.rb for my tests:

require 'capybara-screenshot/cucumber'

Every time a test fails, it captures an HTML output and an image. I’ve discovered that running tests without javascript – which uses the default headless Capybara driver – does not create an image, and also using the Selenium driver in Chrome throws an error, but that hasn’t really caused an issue.

The next step was accessing the details of the fails on the build. I’m using Codeship which is an awesome cloud-based CI platform that offers 100 builds a month for FREE (plenty for a solo developer on a startup!) Codeship allows me to create a debug build and access it via SSH, which was great because now I can generate images and HTML from the failed tests.

The last problem to solve was getting the images. There’s probably a few ways to skin this cat – I started off just using the HTML, but a picture not only tells a thousand words, it usually is a much faster way of seeing the root of the problem than picking through the HTML code. Turns out SFTP works perfectly – I just use the server IP and port from the SSH command that Codeship provides:

sftp -P [port number] user@ip_address
get [image file name]

I do still need to ssh in and run the failing tests, but finally I can see the broken screens! Much faster.

Using Variants in Rails 4

I’ve been using Variants in Rails 4 to change my layouts for one controller action. It’s really straightforward – just set the variant name as a symbol, for example:

request.variant = :popup

Then name the template file with the variant name in it, e.g.


Another neat trick I discovered is that you can also use the variant to control the layout file. I wanted to have a different layout for my popup content, one that didn’t include all of the usual bumph like headers and menus. Turns out I didn’t need any other code, just create a template in my layout folder with the variant:


In addition to that, I can also use it with partials. Inside my haml file, I reference a partial as I would normally, and when the variant is set, it automatically chooses the partial with the variant if there is one.

= render partial: 'customer' 

Now I can use the popup variant across multiple controllers with this same simple layout.

I also discovered that setting variant to nil causes a problem. I wanted to do this:

request.variant = find_my_variant
def find_my_variant
  return nil if some_logic_determining_no_variant

Instead I have to do this:

request.variant = find_my_variant unless find_my_variant.nil?
def find_my_variant
  return nil if some_logic_determining_no_variant

I’m interested to know if there is a way to set a “blank” or default variant so that I can remove that extra check …

Try and Fetch

One of my colleagues showed me this awesome little method in Rails called try.

How many times have you written code to check for nil like this?

def get_value
  if @my_object.nil?

(well I have, many times!)

If you call try on an object if you’re not sure whether or not it will respond to that method. If it doesn’t, then you just get back nil:

def get_value

Particularly useful if you want to call a method on something you get back from a hash that may not be there:


While reading up on this, I also discovered the fetch method for Hashes, which allows me to specify a default value to return if the key is missing.

This means I can clean up stuff like this:

my_array = @my_hash[:my_key]
my_array.each {|array_item| ... some code ... } unless my_array.nil?

To this:

@my_hash.fetch(:my_key, []).each {|array_item| ... some code ... }

Less if statements, less branching, and so hopefully fewer bugs to write!

Controlling Asset Precompilation in Rails

I’ve run into issues recently with precompilation after introducing new gems containing partial SASS files (Bootstrap and Font Awesome).

Rails allows you to specify patterns or Procs to determine which files should – or in our case should not – be precompiled, like this:

Rails.application.config.assets.precompile = [Proc.new {|e| !(e =~ /(font-awesome|bootstrap)\/_/) } ]

Upgrading to RSpec 3

I recently upgraded our Rails application to use RSpec 3, which is currently in its second beta, from 2.99. I was expecting it to be nice and straightforward, but sadly it was not! This was partly because we have been using RR for mocking and stubbing, and the syntax was not compatible with RSpec 3.

In the process of upgrading I learned a bunch about the new RSpec “allow” syntax, which in my opinion is far nicer than the RR one we were using.

Here’s how to stub:

allow(thing).to receive(:method).with(args).and_return(other_thing)

Mocking can be done in the same way by substituting “allow” for “expect” – although in most cases the tests read better if you test that the stub received the method using the following matcher:

expect(thing).to have_received(:method).with(args)

That this is different to the previous RR syntax, which was expect(thing).to have_received.method(args)
You can also use argument matchers, for example:

expect(thing).to have_received(:method).with(a_kind_of(Class))

And you can verify how many times a method was called:

expect(object).to have_received(method).at_least(:once)
expect(object).to have_received(method).exactly(3).times

The .with(args) and .and_return(other_thing) methods are optional. You can also invoke a different function:

allow(thing).to receive(:method) { |args| block }

Or call the original function:

allow(thing).to receive(:method).and_call_original

Another thing we used fairly often was any_instance_of. This is now cleaner (RR used to take a block):

allow_any_instance_of(Class).to receive(:method).and_return
allow_any_instance_of(Class).to receive(:method) { |instance, args| block}

If you pass a block, the first argument when it gets called is the instance it is called on.
In RSpec 3, be_false and be_true are deprecated. Instead, use eq false or eq true. You can use be in place of eq, but when the test fails you get a longer error message, pointing out that the error may be due to incorrect object reference, which is irrelevant and kind of annoying.

Using RSpec mocks means that we can create new mock or stub objects using double(Class_or_name) rather than Object.new, which results in tidier error messages and clearer test code.

Stubbing a chain of methods may also be a handy tool – I only found one place where we used it, but it is useful if we’re chaining together methods to search models.

allow(object).to receive_message_chain(:method1, :method2)

More info:

  1. https://relishapp.com/rspec/rspec-mocks/docs
  2. https://github.com/rspec/rspec-mocks

Update: it turns out I was missing a configuration option in RSpec. It should have worked with RR by doing this:

RSpec.configure do |rspec|
  rspec.mock_with :rr

Thanks Myron for clearing this up :)

Scala – Day 1

I was looking forward to Chapter 5 of Seven Languages in Seven Weeks: Scala. I’ve heard quite a bit about it in the last few weeks at various user groups, and I’m hoping to get my hands on it at some point in my upcoming work with Atlassian, so this was a good time to dive in. As a personality, Scala is assigned Edward Scissorhands in the book: “awkward, and sometimes amazing”.

I tried at first to install it with Homebrew, which just failed with a 404, so I downloaded the package and installed it manually, which worked fine.

Day 1 was pretty straightforward – type a few things into the console and have a look at what you get back. This chapter delves into loops and ranges and compares Scala with both Java and Ruby, finishing up with some simple class definitions and traits. As with most of the chapters so far, it very quickly introduces a lot of ideas – not much detail, but enough to get me thinking.

In the self study for Day 1, the first questions are reasonably simple.

  1. Here’s a link to the Scala API
  2. There are lots of blog posts comparing java to scala, mostly just one aspect. I liked this write up based on a year of experience with Scala.
  3. A discussion of val versus var.

The next challenge was to write a game that will take a tic-tac-toe board (noughts and crosses for the Brits …) and determine a winner. The bonus part of the challenge would be to make it into a game that two people could play, so I attacked this part first.

I started off using Sublime Text 2, then decided to switch to IntelliJ with the Scala plugin. I like Sublime, but was hoping IntelliJ would give me better auto completion, refactoring tools and keyboard shortcuts. It seems to work OK – I had to point IntelliJ towards my Scala installation, and it is still popping up with some errors although it does compile and run just fine. Perhaps in Day 2 and 3 I’ll dig into those a bit more.

In writing the code for the game, I tripped up on a few things. I had Martin Odersky’s book Programming in Scala to refer to as well, which helped me solve most things really quickly.

Firstly that the chapter hadn’t covered how to return a specific type from a function. Scala doesn’t require the return keyword, but if you don’t specify a return value, it returns Unit().

Here’s a function without a return type:

def myMethod() {


And with:

def myMethod() : Int = {


In my next mistake, I tried to create an array and then add items to it – Arrays are mutable in Scala, but you can’t change the size of them. I didn’t even have a good reason for doing this, except that I thought it would make the code prettier, so I changed it to a List (immutable) instead. And it looked fine :)

I don’t know a lot about functional programming yet, so I did have a couple of classes in my solution. I wanted to make sure I had no mutable objects though.

When I finished my initial solution, I had three files – I ended up taking the lazy way of copying all the classes into the one file and running it from the terminal with scala tictactoe.scala. Here’s the initial attempt. I like that it doesn’t have any mutable objects, it’s simple, I don’t have to worry about blanks, and the simple map method to get the positions. I don’t much like the magic winning combinations in the Judge class, and I don’t like that if you don’t enter the moves in a valid format it will barf.

Next challenge: on to Day 2, and also trying to extend the tic-tac-toe game for the bonus challenge!

Frying my brain with Prolog

I recently picked up the Seven Languages in Seven Weeks book again, with the intention to start where I left off with Prolog, around the end of Day 1. As I was doing research for the exercises, I came across a great description that summed up exactly how I felt about this chapter:

“Today, Prolog broke my brain. The chapter started with recursion, lists, tuples, and pattern matching, all of which were tolerable if you’ve had prior exposure to functional programming. However, after that, we moved onto using unification as the primary construct for problem solving, and the gears in my head began to grind.”

At first, it seemed fairly easy to follow, very different to anything I’d done before, but that’s why I started with the Seven Languages book, to learn about new and different techniques in programming.

Reading through the day two section about lists and recursion, I started to find myself getting lost in the examples and it took a long time to understand what was going on. I couldn’t complete the day 2 exercises without a little help from the interwebz, although by the time I finished working through them, I did understand what I was going on. Trying to switch my brain from thinking about solving the problems in terms of rules rather than algorithms continued to bite me throughout the chapter though.

Some of the things I learned through Day 2 are pretty basic, but for a complete newbie to Prolog, they weren’t obvious.

In the factorial exercise, I realised that within a rule, I could add a line to validate the parameters – in this case that X > 0. Super obvious maybe, but Day 1 was all about matching rules and so this was new.

The next thing I learned was that you can have two versions of a rule with different conditions. I was already creating multiple versions of a rule to unify specific values such as 0 for factorial, but this was a different way to think about it.

As I worked through the sudoku and queens exercises, I still found myself wanting to do something like this:

Diags1 = [R1+C1, R2+C2, R3+C3, R4+C4, R5+C5, R6+C6, R7+C7, R8+C8],

… which just doesn’t work!

I did get there in the end with the queens solution, with a little help from the book to point me in the right direction for the diagonals.

In conclusion, I definitely learned a lot from this chapter, but struggled a lot as well! It was worth fighting through to the end though, as the concepts did start to make sense!