Lesson Learned

GET and POST variable hashes in Ruby on Rails

In Rails, you access GET, POST, and routing variables through the params hash. This works almost all the time, except when you duplicate a variable name: routing overwrites GET overwrites POST.

For an app I'm working on I actually had to care where a variable comes from, so I dug for a while to find out how to access the raw hashes. It ain't pretty, but here it is in case anyone else ends up needing it:

get = CGIMethods.parse_query_paremeters(@request.query_string)
post = CGIMethods.parse_query_parameters(@request.raw_post) 
# you'd think you could use @request.query_parameters and
# @request.request_parameters, but they're update()d by route vars
route = @request.path_parameters

(Also, don't ask about this in #rubyonrails -- you'll just get lectured on how you don't really want to access the hashes, how you should rename all your variables and URLs, and how it simply isn't possible. This will be very frustrating and totally unproductive.)

Rails Makes Valid XHTML Easier

I'm working on a Rails site in my Copious Free Time and I wanted to share a little way that Ruby made my life easier. I'm making my pages valid XHTML 1.0 Transitional because it makes life easier to find bugs and it just feels good to know I'm meeting the spec.

The W3C Validator complained that I didn't have the rows and cols attributes on my <textarea> tags. My code for them looked like:

<%= text_area_tag :message, params[:message] %>

And I don't want to add the :size option because I use CSS to style all of them, it'd be confusing to see an unused size there. So I extended the text_area_tag method in my app/helpers/application_helper.rb to fill in a default:

module ApplicationHelper
  def text_area_tag(name, content=nil, options={})
    super(name, content, { :size => "40x10" }.update(options))

What does Ruby on Rails Have to Offer a "Java Guy"?

Alot....

I have been working as a "Java Guy" for roughly five years now. The majority of my work has been component / library development, and it has been enjoyable on the whole. However, I have also had a few unfortunate “Java web” development projects. These experiences have not been something I hope to repeat often. I am not going to re-describe the Java / J2EE deficiencies in the Web UI layer, but I will go over a few examples of how messy it can get and why Ruby on Rails makes me smile. :)

Tidy Stylesheets in Rails

It's very easy for a site's CSS to grow a single giant, brittle stylesheet. It becomes impossible to change anything because of bizarre interactions between elements, unexpected interactions, and simply because it's just too big for anyone to understand. Much of programming is managing complexity, and I'll share a nice technique in that vein.

I like to break down stylesheets so there's a site-wide stylesheet with global stylings like fonts, the site's template, and common elements. This is the file that metastasizes on you.

My solution is to break down stylesheets by controller and action, and Rails makes this quite easy:

# app/views/layout/application.rb
<%= stylesheet_link_tag *(['global'] + @stylesheets) %>
# app/controllers/application.rb
class ApplicationController < ActionController::Base
  before_filter :add_stylesheets

  def initialize
    @stylesheets = []
  end

  def add_stylesheets
    ["#{controller_name}/_controller", "#{controller_name}/#{action_name}"].each do |stylesheet|

Contest Voting: Which person made the best case for their technology person?

<a href="http://codesnipers.com/?q=node/473">Steve Moyer's</a> appreciation of Jack Crenshaw?
5% (2 votes)
<a href="http://codesnipers.com/?q=node/472">jwikholm's</a> dedication to Craig Larman?
45% (19 votes)
<a href="http://codesnipers.com/?q=node/481">Nola Stowe's</a> discussion of Keith Casey?
38% (16 votes)
<a href="http://codesnipers.com/?q=node/482">Gavin Bowman's</a> dedication to the mISV Community?
12% (5 votes)
Total votes: 42

Contest: The Micro ISV Community

This is the fourth and final of the contest entires. Voting begins tonight in less than 4 hours!

I tried thinking hard about my entry to this contest, and I tried to think of a list of technology people who had influenced me, but it just wasn’t happening.

I stumbled into technology, so I couldn’t pick a single shining light that has driven or shaped me and my career. It’s rare that I make it all the way through a technical book. I’ve always tried to learn enough to accomplish the current task and then hoped to come back to it later, but later never comes. I’m trying to figure out whether this makes me a really bad person to give free books to, or whether I really need it, I’ll let you guys decide for yourselves.

So I’m thinking back over the road that brought me here, maybe I’ll find a winner there. Maybe I should pick a childhood neighbour. I remember playing Frogger and Manic Miner on someone’s Spectrum, which in turn made me bug my parents to get me one. Maybe I should choose my parents for humouring me and not forcing me to go and play outside? When I started work, there were various people who gave me a chance, mentored me, or steered me towards software development. There are a few candidates there.

Contest: The Technology Person Who Has Had The Biggest Impact On Me

This is the third of the contest entires. Voting begins tonight at 5pm EST.

I wasn’t going to enter, because the person who probably has had the most impact so far on me is Keith Casey. Oh yeah what a kiss up!! You’re just writing this because he is running this site and you want to get on his good side! … no, I’m not writing this to win. I’m just writing about him because I would feel dishonest writing about anybody else.

I knew Keith from when we were teenagers. I would see him occasionally since those day, but we never talked too much and certainly not about programming! One day, I hear he is looking for PHP programmers. Hey, I do PHP. I dream in PHP. So I shoot him an email and I start working as a contractor for CaseySoftware.

The two years before this, however, I was sort of in a “programmer depression” where I didn’t even actually check my personal email at home. I didn’t do any consulting, no freelancing, no “talking” about coding with anyone. I was a lone developer working in an environment that was not very progressive.

Contest: Craig Larman - An Agile Evangelist

This is the second of a series of contest entires. Voting begins on Friday 08 September.

After some very serious pondering I have come to the conclusion that the person who has had the most profound impact on me has to be Craig Larman. Although there are of course several movers and shakers that have influenced me, Mr. Larman is the one that really has made me think differently about software development.

Craig Larman is the author of "Agile and Iterative Development: A Manager's Guide". At first the book title felt repelling since it was directed at managers and I certainly am not one. Fortunately for me I read the book - I devoured it in a few nights. The concepts of Agile software development seemed so perfectly sane and logical that I was shocked that I hadn't thought of them before. This image of a this-is-for-your-own-good kind of down-to-earth mentality was further enforced when I got to hear Craig speak at the second ever Agile Finland seminar.

Craig Larman has permanently shifted my brainwaves by such a degree that I will not - without influential coercion - go back to the Dark Age of Software Development from which I was once freed.

"Muntz" your code

A long time ago, when I was a much younger engineer, I read an article in "Electronic Design" magazine by Bob Pease of National Semiconductor titled "What's All This Muntzing Stuff, Anyhow?". In this article, he describes how Earl "Mad-man" Muntz, would clip parts out of his engineer's circuits to see if the parts were absolutely necessary. As a consequence, his television sets cost dramatically less than his competitors. This article is definitely worth a read (as are Bob's other articles).

Flintstoning Toasters

Flintstones Record Player
I picked up the term "flintstoning" from my visit to Cambrian House. It's the practice of substituting a little human work for functionality until there's enough demand for the feature that it's worth the coder time to implement. Let me give you an example.

You're a web coder for a bank whose promotion this month is a free toaster to everyone who deposits $10,000 to open a new account. The bank realizes that toaster manufacture and delivery is not their core competency, so they outsouce the task the lowest-bidding toaster fufillment processing agency. Your job is to write the code to get toasters to web customers. You have two options:

  • Spend painful hours attempting to reconcile the inconsistencies between the toaster pimp's documentation and their Java-powered full-stack WSDL automated toaster delivery processing gateway until XML angle brackets gouge your eyes out.