Naked Hashes are Trouble

24 Nov 2008

So I had this form in a Rails view that needed some changes so, amongst other things, I changed this:
form_tag(:action => ‘search’)

to this:
form_tag(:action => ‘show’, :method => :get)

I had noticed that the show and search methods essentially did the same thing (and had the same views) – which I why I refactored. However, when I made the change I got a 500. I was using restful routes and this form needed to be a get – which I thought I had indicated. But I had not – what should have put in the view was this:

form_tag({:action => ‘show’}, {:method => :get})

There’s this idiom in Ruby where we don’t put curly braces around hashes if we don’t need to, but I’ve consistently lost a bunch of time to mistakes like the one above. So I’m thinking about explicitly denoting hashes with curly braces in my future Ruby code just to improve intentionality.