Naked Hashes are Trouble
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.
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.
Comments
There is no obvious difference between changing :x=>1 to :x=>1 :y=>2 and {:x=>1} to {:x=>1,:y=>2}
The only problem-solving method would be to use {:x=>1},{} for every call, but who wants to do this...
You could actually write you code to look like:
form_tag({:action => :create}, :method => :post)
And it would still be valid.