This Month
July 2006
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Year Archive
Login
User name:
Password:
Remember me 
View Article  RESTful tests

If you're drinking the latest flavor of Rails kool-aid, you are no doubt hard at work rewriting all of your controllers to conform to RESTful principles.  Good for you!  In your rush to coolness, however, don't forget to do the janitorial work of testing those great new actions.  I recently added Accept-based processing to one of my controller actions, and I wanted to test the XML response separately.  I couldn't figure out how to send the Accept: header in a test GET request for the life of me, until I stumbled upon Francois Beausoleil's post about respond_to order.  The simple answer:

@request.env['HTTP_ACCEPT'] = 'application/xml'

Stick that into your test method right before your get / post, and you should be good to go.

Tags: , , ,

View Article  Caring about URLs in your Rails app

If you're a Rails developer who wants users to actually visit your application once in awhile, it would help to pay some attention to the little white box at the top of your browser.  Right now, it probably says:

http://localhost:3000/bacon/show/31

While the URL appears clean to us programmer-types, there's plenty of room for improvement.  The main problem is that the ID, 31, has meaning only in the context of your application.  When the URL shows up somewhere else, it's just a number.  This matters a tiny bit to humans, and a lot more to search engines.  We'd like to put something there that tells what 31 actually is.  In my last app, my first attempt at URL beautification looked like this:

http://localhost:3000/bacon/show/Wood-Smoked

That's a good start: it loads the URL with more information about what's being displayed, and the dash as word separator is search engine friendly.  However, this setup still has problems:  what if two bacons have identical names?  This actually happened in my app, where several categories were named "General".  Also, what if we change the name of a bacon?  Then, references to the old name in the URL won't work.  That stinks!

I'll spare you the further thought exercise and say that someone else has already done the hard work for you.  His post on Transparent opaque changeable permanent URLs is a masterful dissection of both problem and solution.  In short, his solution is to put both the id AND the title / whatever into the "slug" (that thing at the end of the URL that we're concentrating on), and to redirect users if they get the ID right but use an old or incorrect title.  Sweet!  If you're like me, however, you wished for one more thing: spoonfed Rails code.  I found some of that, too, at URLs on Rails.  Finally, the author mentions implementation of the redirect for incorrect slugs but doesn't show the code.  He's right, it's easy, and here's how I did it:

@bacon = Bacon.find(params[:id])
if params[:id] != @bacon.to_param
  headers["Status"] = "301 Moved Permanently"
  redirect_to :id => @bacon and return
end

Now you should have all the code you need to build bulletproof, search engine-friendly URLs into your app.  Enjoy the bacon!

Tags:

View Article  Looking back, seeing far
To start, a little shameless self-promotion:  the Eponym Blog Directory was officially and for real opened up yesterday, to the wild cheers of... well, just me.  Its unveiling coincides with the upgrade to the Eponym Blog Search, which was pretty much a requirement since the two sites are tightly linked.  I'd encourage you to check both of them out, and let me know what you think.
One of the nice side benefits of working on the directory is that I get to view a variety of good blogs.  One in particular caught me today, both for its content and its intent.  Ten Years of My Life is a photoblog in which the author intends to post (at least) one photo per day for ten years.  Since the author in question is both a dad and a sweet photographer, it's a very enjoyable photoblog.  Beyond that, his stated goal (to chronicle ten years of his life through photos) strikes me as a great gift to one's descendants.  Your kids may inherit a stack of vacation photo albums and several tapes / DVDs / reels of home movies, but 3650 photos taken over ten years will tell stories about your life that none of the other stuff does.  Check him out.  No, seriously, check him out!
View Article  The Madness of Crowds
Rarely does a day go by that someone doesn't lament the current state of discussions on Slashdot.  I didn't expect ...   more »
View Article  Go Fly A Kite
Yay, just what I needed: another hobby!  While on a recent vacation to Eureka Springs, we stopped by Kaleidokites...   more »
View Article  My Very First Rails Patch
The other day, I ran into an odd problem with Rails migrations.  I needed to add an index to a MySQL TEXT column, and such indexes are required to include a prefix length (who knows why).  Anyway, I discovered that I couldn't just tack the prefix length onto the column name because ActiveRecord was blindly quoting the entire string.  I carped about it lightly on the #rails IRC channel, and forgot about it for a few weeks.  Today I decided to take a few minutes and see how hard it would be to fix that little wart.  Turns out it wasn't tough at all.  Behold my first [PATCH] ticket!  It's not exactly RJS, but hopefully someone will find it useful (and hopefully it won't break anything else).

Tags:
View Article  "We do more before 9AM than most people do all day."
"Have you ever had a dream, Neo, that you were so sure was real? What if you were unable to wake from that dream, Neo? How would you know the difterence between the dream world and the real world?"   more »