Ruby On Rails – Not As Happy
By Adrian Sutton
So I’ve been getting into Ruby on Rails and things started out well, had the basis of an application up pretty quickly and now I’m starting to get into the logic of the app rather than just pulling values out of the database and displaying them. Productivity is droping, very rapidly.
The problems all seem to come down to the lack of a good IDE. Specifically:
- I have no idea where the definition of methods etc are. Are they inherited? Are they dynamically generated by something?
- I have no idea where the documentation for stuff is. Is it at api.rubyonrails.com, is it on ruby-doc.org, is it buried somewhere in my gems installation?
- Navigating files is just too slow.
It wouldn’t take much of an IDE to fix those problems just display a nice list of files in the project down the side with tabbed editors, then have the ability to select a method, variable, etc and jump to it’s definition or it’s documentation.
Other than that the lack of documentation and/or the lack of organization to the documentation is really getting annoying. Often the documentation is out there, you just don’t know how to find it. Sometimes the documentation is just not comprehensive enough, is too vague or ambiguous.
There’s also a few weird things that come up from time to time and working out what went wrong is very difficult. For instance, I wanted to check over user input before saving it to the database to avoid rouge HTML tags causing problems etc. So in the appropriate controllers I wrote (among many other attempts):
def before_save hash = Hash.new attributes.each_pair do | key, value | if !value.nil? and value.is_a? String hash[key] = filterHTMLTags(value) else hash[key] = value end end attributes = hash end
filterHTMLTags is defined and does what it sounds like. You’d think this would filter any string values before saving them to the database, but it’s effectively a no-op. The values are saved to the database in their unsafe form.
If however you make the last line:
self.attributes = hash
It works. I assume the first version made a local variable called “attributes”, assigned hash to it and dumped it when it went out of scope (@attributes didn’t help here either btw). self.attributes seemed to kick Ruby into method call mode and called attributes= with hash as an argument so it stored correctly. I could understand this behavior if I also had to say self.attributes to get the value, but it’s very odd to have the behavior differ.
Of course I’m clearly just misunderstanding things and these learning pains will reduce as I learn more about Ruby and Rails (though the file navigation problem is only going to get worse) but it’s worth noting that this is definitely no panacea and there are still gotchas and time sinks hidden around the place.