Close and Go BackBack to Viget

I Love Me Some Skitch!

Patrick Reagan
Patrick Reagan, Development Director, January 29, 2009 4

I'm a late adopter, it's true. I have a cell phone from earlier this century, I only just upgraded to Leopard in the last 3 months, and I'm just now discovering the usefulness of Skitch. It rocks!

Everyone knows about the web posting functionality, but for me I find drag & drop to be the most useful feature – especially when paired with the Blogging Bundle in TextMate. If you don't have it installed, grab it with the GetBundles bundle (Al3x has a good write-up on this):

Text Mate — get Bundles

Once you're set up to post to your blog, you can easily drop in an annotated screen capture:

Meta Skitch

If you're not already blogging with TextMate, go do it now (if you're using Expression Engine, we've got you covered). If you're not using Skitch to quickly embed images, you should give it a shot.

Rails Sessions Not Working with Safari? A Solution …

Matt Swasey
Matt Swasey, Web Developer, January 23, 2009 3

Having trouble setting session variables in Safari and IE, but not Firefox?  I thought I'd share this quick tip to help save you some time and frustration. 

Bottom line: Don't use underscores in your hostname. Read on for more information.

The Problem

I was working on a rails application locally using passenger -- my development url was http://ux_prototype.local. I had everything working in my development environment so I pushed it out to production for April to check things out. Everything looked great and we were ready to roll.  Or so I thought... 

The app was throwing 500 errors in strange places whenever we were testing in Internet Explorer. 

Using Firefox, I was cruising along with no problems. Checking in Safari  from time to time things looked fine.   In Internet Explorer, things looked ok at first, however, when making a request for an action that altered session data (logging in or out), the 500 error appeared.  Going back to Safari I noticed that, in fact, it was having the same problems as IE.

Neither server-side clearing of session data (ActionController::Base#reset_session) nor client-side deleting the cookie from browser preferences helped at all. Using ./script/server to start Mongrel, however, did help. Could it be the hostname?

The Solution

Having a clearly defined problem revealed better results. As soon as I changed the hostname to http://ux-prototype.local, the problem went away.

So, don't use underscores in your hostname. While underscores are now considered "valid," many DNS clients and browsers will react with varying results. While this may be obvious to some, it created some frustration on my part and could easily be sidestepped in the future by avoiding this situation altogether.

Heres hoping it might save you some time and frustration as well!

Getting (And Staying) Motivated to Code

David Eisinger
David Eisinger, Web Developer, January 21, 2009 5

When you’re working on code written by another programmer — whether a coworker, open source contributor, or (worst of all) yourself from six months ago — it’s all too easy to get frustrated and fall into an unproductive state. The following are some ways I’ve found to overcome this apprehension and get down to business.

Tiny Improvements, Tiny Commits

When confronted with a sprawling, outdated codebase, it’s easy to get overwhelmed. To get started, I suggest making a tiny improvement. Add a named scoped. Use a more advanced enumerable method. And, as soon as you’ve finished, commit it. Committing feels great and really hammers home that you’ve accomplished something of value. Additionally, committing increases momentum and gives you the courage to take on larger changes.

Make a List

In Getting Things Done, David Allen says,

You’ll invariably feel a relieving of pressure about anything you have a commitment to change or do, when you decide on the very next physical action required to move it forward.

I like to take it a step further: envision the program as I want it to be, and then list the steps it will take to get there. Even though the list will change substantially along the way, having a path and a destination removes a lot of the anxiety of working with unfamiliar code.

To manage such lists, I love Things, but a piece of paper works just as well.

Delete Something

As projects grow and requirements change, a lot of code outlives its usefulness; but it sticks around anyway because, on the surface, its presence isn’t hurting anything. I’m sure you’ve encountered this — hell, I’m sure you’ve got extraneous code in your current project. When confronted with such code, delete it. Deleting unused code increases readability, decreases the likelihood of bugs, and adds to your understanding of the remaining code. But those reasons aside, it feels great. If I suspect a method isn’t being used anywhere, I’ll do

grep -lir "method_name" app/

to find all the places where the method name occurs.

Stake your Claim

On one project, I couldn’t do any feature development — or even make any commits — until I’d rewritten the entire test suite to use Shoulda. It was mentally draining work and took much longer than it shoulda (see what I did there?). If you need to add functionality to one specific piece of the site, take the time to address those classes and call it a victory. You don’t have to fix everything at once, and it’s much easier to bring code up to speed one class at a time. With every improvement you make, your sense of ownership over the codebase will increase and so will your motivation.

In Closing

As Rails moves from an upstart framework to an established technology, the number of legacy projects will only increase. But even outside the scope of Rails development, or working with legacy code at all, I think maintaining motivation is the biggest challenge we face as developers. I’d love to hear your tips for getting and staying motivated to code.

Simple Features that Set Emacs Apart

Clinton R. Nixon
Clinton R. Nixon, Development Director, January 19, 2009 3

I started using Emacs as my primary text editor about three years ago. After two weeks, it quit being a difficult experience. When I joined Viget and started programming in Ruby again after a stint in PHP, I shifted to TextMate, but that didn't last long. There are features in Emacs that I wanted all the time. Those features were not only what I'd grown accustomed to, but also were major time- and effort-savers. Here are the three features I missed the most. Even if they don't convince you to try Emacs, they can be helpful in evaluating whatever text editor you do use.

Buffers

If you have ever used tabs in a program interface, the basic idea of buffers will be familiar. Each buffer is a separate workspace that you can switch between quickly. Unlike the tabs in most text editors, though, Emacs' buffers are not limited to containing files. They can contain files, scratchpad areas, shells, output from a running process, and anything else Emacs can access. This allows me to have autotest running in one buffer, bash running in another, irb (or script/console) in a third, and the files I'm editing in the rest.

Having all my development tools inside one program is nice, but what makes buffers a killer feature is the ability to switch between them quickly. You've probably heard of Emacs' storied multi-key commands: to switch buffers, you use a simple one, C-x b. (C-x just means "hold down Control and hit the x key.) Using this command will give me a list of buffers I can drill down into by typing a few characters from the name of the buffer I want, much like Quicksilver works. This may not seem like an improvement over typing Cmd-[ and Cmd-] (or whatever it is on your platform) to transverse tabs, but when you've got 30 buffers open, it's a godsend.

The Kill Ring

"The kill ring" is a terrifying name for a simple concept: whenever you cut or copy text in Emacs, it puts the text in a ring, or circular array. (We use different terms for cutting, copying, and pasting in Emacs, but I'm going to use the common terms.) When you paste text, the last item put on the ring is pasted, but you can then iterate through the ring until the item you want to paste comes up. This helps a lot when I'm refactoring code: I can grab the bits I want to move in separate chunks, move to another area of the file or another file, and paste each chunk where I want. Pasting doesn't remove the text from the ring, so I can also bring back something from ten minutes ago if I need it again.

Even if you don't use Emacs, you can get some of this functionality with a better clipboard manager for your platform. I use Jumpcut for OS X.

Emacs key commands

Much ballyhoo has been made over the years about Emacs' key commands, claiming you have to use all ten fingers at once to get something done. The exaggerations aren't completely off-base (I have it on good faith that "Emacs pinky" is a real ailment, and I type C-c ; f t to jump to a test from a model or controller in Rails), but they hide the real power of this system.

Emacs' basic key commands are available in many other places on your computer system. If you're using a Unix of any sort, your shell probably supports them. If you're using OS X, any text field also supports these key commands. The basic commands that may work on your system are:

  • C-a: go to the beginning of the line
  • C-e: go to the end of the line
  • C-f: move forward a character
  • C-b: move backward a character
  • C-p: move to the previous line
  • C-n: move to the next line
  • M-b: move back a word
  • M-f: move forward a word
  • C-k: delete everything after this point
  • C-d: delete the next character

M-f means "Meta-B," which in Emacs moon-language, generally means "Alt-B." In your shell, Emacs' cut, copy, and paste commands work as well.

Once these key commands were hardwired into my brain -- which only took two weeks -- I would reflexively hit them no matter what application I was using. Having this work most places was nice; suddenly having better movement, deletion, cut, and copy powers in my shell was amazing.

There are many other features in Emacs that make my work easier every day, but these three alone are enough to elevate Emacs over any other text editor I've used. In my next post, I'll talk about Emacs features that make it not only a great text editor, but a good development environment.

IE, jQuery, Rails, and HTTP, oh my!

Ben Scofield
Ben Scofield, Technology Director, January 16, 2009 4

We've been using jQuery more and more lately; thanks to the helpful jRails plugin, our transition from Prototype has been nearly seamless. Recently, however, I ran into a persistent problem with my $.ajax calls in IE -- namely, that GET requests were coming across as POSTs (to both Apache and my application), each of which generated a MethodNotAllowed error for the particular URL it was hitting. In Firefox and Safari, of course, the requests came through as the GETs they were supposed to be. After a long search and several false leads, I finally discovered that the culprit was the following bit at the bottom of our application.js:

$(document).ajaxSend(function(event, request, settings) {
  if (typeof(AUTH_TOKEN) == "undefined") return;
  // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
  settings.data = settings.data || "";
  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});

We use this code to handle Rails's built-in CSRF protection (as detailed here), but it turns out that in IE, adding to settings.data in this manner automatically turns your request into a POST, no matter what else you might specify. Once we found this, it was a simple matter to fix it; since CSRF protection (and the authenticity token) aren't needed for GET requests, we can exit out of the function before any harm is done in those cases:

$(document).ajaxSend(function(event, request, settings) {
  if (settings.type == 'GET' || settings.type == 'get' || typeof(AUTH_TOKEN) == "undefined") return;
  // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
  settings.data = settings.data || "";
  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});

And like magic, everything just works in IE as it should.

We're the Developers

at Viget Labs. We write about web development trends, tips, best practices, industry events, and our projects — all with an emphasis on Ruby on Rails.

Recent Comments

For translating strings you can use Rails I18n backend instead of using inflectors.

The `typus_human_name` is a patch to fix a problem in `human_name` [1].

[1] https://rails.lighthouseapp.com/projects/8994/tickets/2120-humanize-and-human_name-dont-separate-words

Contact Us

Have any questions, comments, ideas, or secrets to share? Let us know.


Sorry, you need to have Javascript enabled to use this form. (Don't blame us, blame the spammers!) If you'd like to contact us, please visit our Contact page.