Ruby on Rails

A collection of our experiences and tips on working productively with Ruby on Rails.

Articles on Ruby on Rails

Default environment specific values for DATABASE_URL

Setting your database setting in an environment variable makes sense, but can be a pain sometimes as it breaks Rails' assumptions. Check an easy way to get the best of both worlds.

Read more on how to default DATABASE_URL to your database.yml settings

Simplifying ActiveRecord Connection Strings

Over the years I've gone to great lengths to avoid committing production passwords into version control. With Rails this has inevitably meant a number of different ways to ensure the database.yml was where it needed to be on the production servers, with the right credentials. Custom Capistrano recipes to create a new config when setting up a server, moving it into place after a deploy, it worked well enough but it's always felt a little heavy.

Read more on how to simplify ActiveRecord connection settings

You don't win friends with salad

Trying to work out which testing framework to use in ruby these days is simple, all the kids are using cucumber so you should too lest you want to be an outcast. It's all very great in theory, but how much is it really helping you?

Read more on how to remove cucumber from your diet

Upgrading Engine Plugins to work with Rails 3

Upgrading to Rails 3 is pretty quick and easy, but there are a few gotchas if you want to take your Rails 2.3 Engines with you.

Read more about Upgrading an engine to work with Rails 3

Rails Underground - Day 2

update: videos are now online I was a little late to the kick-off of the keynote on day 2 with Yehuda. For the most part if is another discussion on what's happening in Rails 3 so I don't think I've missed anything I hadn't heard before. He then started to go into more detail on how things have changed than he has previously, probably due to the progress since I last saw him in March… Yehuda went through the upcoming changes in approach and philosophy on rails Dev. Having well…

Read more about Rails Underground - Day 2

Rails Underground - Day 1

update I've also got summaries for Rails Underground - Day 2, plus videos are now online Today kicks off the first day of talks for Rails Underground in London. Thankfully they've got usable wifi, and two days of what sound like mostly excellent talks. So over the next couple of days I'll hopefully be able to post summaries of the various talks I'm able to squeeze into. So first up… Taking a queue from Martin Fowler's "Patterns of Enterprise Application Architectures" Fred goes in to discuss the various…

Read more about Rails Underground - Day 1

Scotland on Rails 09 - Videos are up

Not much more to say really, the title sums it up. For those that weren't there or those that were that just want a recap go and check out the Scotland on rails videos now. And I will eventually get around to doing that round-up of the post/out-of-conference activities to do should you be looking to attend the conference yourself next year. …

Read more about Scotland on Rails 09 - Videos are up

Capturing a form cancel

I've been meaning to write this one up for months now, ever since I originally posted the sexy forms in rails. The form builder I talk about in that post makes it easy to put a submit and cancel button on a the form, and a few people have asked how to handle the cancel action. Some have suggested using javascript to redirect back using an onclick, ick! What happens if the user doesn't have javascript support? Sure you might try and justify it by saying, "Well, how many users don't have javascript…

Read more about Capturing a form cancel

Rails Hosting - Updated

This is just a quick note so that those subscribing to my feed or checking the site for updates are aware. I've made a series of minor changes to the rails hosting post from last month based on the feedback provided by you guys. If you've got any additional experiences with different rails hosts that you'd like to share, then please send them through. …

Read more about Rails Hosting - Updated

Ruby on Rails Tutorials

There has been a few occasions over the previous months when various people I meet have asked me to point them in the right direction to get started with ruby on rails. I remember when I started, it was a pretty easy question to answer… everyone just got pointed in the general direction of why_'s poignant guide, the pickaxe book, and agile web development with rails. But things have come a long way since then. There is not only a veritable cornucopia of options now out there, but for somebody who isn't already engrossed in…

Read more about Ruby on Rails Tutorials

Custom Starting Templates for Rails Apps

Okay, so I've been a little slow on the uptake on this one. But for anybody else that missed it, you can now define your own standard setup for a rails app. (And kudos to Peter Szinek, who I work with on the leading ruby web spider/data scraping library, for the heads up on this last week) That's essentially what this change does. Most of us probably have out preferred setup, especially if we've deployed a few sites. Typically I create the rails directory structure, then pull in HAML, rspec, rspec on rails…

Read more about Custom Starting Templates for Rails Apps

Ruby on Rails Hosting Round-up

UPDATE: I've extended this post based on the feedback in some of the comments. So I haven't used all of the rails hosts listed here, but I'll make it clear which those are. Working for various clients and along side various colleagues over the years, I've been witness to a whole number of hosting providers in the ruby/rails space. Some are great, others acceptable, and a few were just down right terrible. So how do you go about choosing which host is the right one for you? Read on… One…

Read more about Ruby on Rails Hosting Round-up

Sexy Forms in Rails

I've been meaning to release this for quite a while now, I've finally got around to packaging it up for some form of public consumption. Basically, I got sick of having to manually create label tags for each of my form inputs. They should be there by default so that users with visual or other impairments have enough additional information to use the site. I'm also lazy, and would have a tendency to forget to put them in otherwise. I also wanted a consistent way to display any additional contextual information, so here it is. I'll…

Read more about Sexy Forms in Rails

Useful Flash Messages in Rails

Inevitably somewhere in your rails app you display flash messages to your users to inform them that an action has (or hasn't) taken place. Sometimes they need to provide more info, maybe you've just created some new information for them. Why make them sit around, or wonder where they need to go next when you can take them straight to it in the message itself? I've ripped this code out of a project I've been working on and made it a little more generic. Initially I was going to just insert a link in the flash…

Read more about Useful Flash Messages in Rails

Make your own IM bot in Ruby, and interface it with your Rails app

In a super secret project I'm currently working on, I've been looking for ways of making it easier for people to interact with the system without the need to log in to the website. That's included the obvious things like having a RESTful API so they can put their own services and interfaces on top, but that only works for developers or 3rd party application providers. What about your average Joe on the street, how come they get forced into having to log in to the site to update their data, etc.? Why not provide a means…

Read more about Make your own IM bot in Ruby, and interface it with your Rails app

Bulk insertion of data with ActiveRecord

Apologies to all for the extended hiatus, I was in the US for a month on holiday; and then I just got lazy. Anyway, I'm back and plugging someone else's extension. I came across ActiveRecord Extension last night, and was quite frankly dumbfounded that 1) I hadn't come across it before, and 2) More people weren't using it. In short, it will let you use ActiveRecord to batch insert multiple rows in a single query, rather than looping over the Model#create method and getting all the overhead that comes with it (and the hundred insert…

Read more about Bulk insertion of data with ActiveRecord

Letting nginx automatically detect new rails apps

Following on from my post yesterday explaining how to let god automatically detect any new rails apps is few capistrano and config tweaks. This time the changes will let nginx use the same config file we created to so that setting up a new upstream and rails config is as simple as a call to deploy:setup in capistrano. These changes have a dependency on the changes from my previous post, so be sure to follow the steps on creating app.god to dynamically configure god for rails Within your deploy.rb you want to insert this snippet: I'll…

Read more about Letting nginx automatically detect new rails apps

Rails god config

If you're anything like me, you're not a big one for shelling out needless money to host the various development sites your working on. That inevitably means you'll end up with a whole host of small applications running on the one server, each needing a mongrel or two, and of course you want to make sure they are up and running. Thanks to the wonder that is ruby (and god) we can easily set it up to automatically become aware of any new apps we deploy via capistrano. Don't stress if you've never heard of…

Read more about Rails god config

17 Rails plug-ins you should be using

So you're pretty happy with your rails-fu, but are you using all the plug-ins you could be to make life easier for you and your users? The following is a list of plugins I use on almost all, or if not the vast majority of, projects I work on these days. As soon as I've setup my rails directory tree, I install these bad boys too. …

Read more about 17 Rails plug-ins you should be using

Improved timezones in Rails 2.1

The latest release of rails has made the handling of timezones in rails even easier. No longer do you need to install a couple of plugins, although you still need the tzinfo gem. With the new baked in support for timezones in rails you can ignore the previous article on how to support timezones in rails 1.2.x. Now all you need to do is set the desired timezone you want to be working in: Behind the scenes the date will be saved as UTC, but the appropriate timezone conversion will happen for you whenever you output a date…

Read more about Improved timezones in Rails 2.1

Improving rails app and mongrel performance with Thin

Thin is a replacement webserver for mongrel* that further improves the already lightening performance of mongrel. Installation is a breeze, performance is fantastic, but it's still in alpha so use with caution. As you can see in the chart above (taken from the Thin website, I've not had a chance to verify independently), the improvement while handling 10 concurrent connections isn't exactly an order of magnitude larger. But you'll see that as concurrency increases, performance holds steady unlike using mongrel. This is because it's using EventMachine network I/O library to handle incoming requests much…

Read more about Improving rails app and mongrel performance with Thin

Free ATOM feed loving

Chris over at Err the Blog have a great article about how you can very quickly and easily create ATOM syndication of your content with a gem called mofo. I'll avoid any misrepresentation by taking the definition straight from the microformats site There are already formats defined for people, places, and business cards, events and calendaring, voting, reviews, friends and connections, and tags to name but just a select few. Well here is what the entry for this article in the main page listing would currently look like: And all it would take to turn it into a microformat…

Read more about Free ATOM feed loving

New Error Handling in Rails 2.0

Rails 2.0 has added in a very cool way to trap errors without a bunch of messy nested rescue blocks or case statements. The ticket that added all the magic is here. And it couldn't come too soon as I immediately had a project that needed it. Check out what I was looking at doing: and now with the new exception handling: woohoo!! Much cleaner and more descriptive of my intentions …

Read more about New Error Handling in Rails 2.0

Testing XML output in rspec and rails

Have you ever tried testing that you get the expected XML output from your rails respond_to block? Did you take the lazy way like me and think it would be as simple as comparing the body of the response with a string matching the expectant XML? Well there is a problem, if you've got multiple attributes on any elements you can't always guarantee the order that the attributes will be output. So lets do it properly, and check it is valid XML and has the attributes we want. I was working on a project needed a graph of…

Read more about Testing XML output in rspec and rails

Correctly handling multiple time zones in ruby on rails

A recent client I was working for had the unfortunate situation where we'd both independently assumed we understood the requirements, without realising a missing piece of complexity. It was around time zones. The system was implemented with a cut-off date being US EST, but most of their clientele are in LA so it needed to be US PST. What is a developer to do? Well it turns out that timezone_fu will come to the rescue. Getting timezone_fu working with an existing rails app is a cinch, the only dependency is on the TZInfo package, so best you grab…

Read more about Correctly handling multiple time zones in ruby on rails

Simplifying rails controllers with make_resourceful

See the content of the picture above? Wouldn't it be wonderful if you could reduce all of your controllers down to 3 lines? Well you can't do all of them, but if you are adhering the a RESTful approach then chances are most of the controllers can be condensed considerably using make_resourceful. Firstly I have to thank Jeremy McAnally for putting me onto this wonderful new plugin for rails. He used it in a project we were working on together and I've used it almost everywhere since. It's from the Hampton Catlin who is also known…

Read more about Simplifying rails controllers with make_resourceful

Engine Yard Bounty Pays Off

Engine Yard, the current big daddy of rails hosting, decided to put a bounty out to solve a problem all the newly converted rails/nginx users would have to some degree but have previously been ignoring. The load balancer in nginx works in a round robin fashion meaning that any long running request has the potential to cause another user to queue behind it, rather than getting served by one of the free and waiting mongrels. Well wait no more! Take a look at Ezra's blog for the most definitive run down on how to get it working for…

Read more about Engine Yard Bounty Pays Off

Reject junk email addresses with email veracity

Carsten Nielsen has created an amazing plugin for rails called email veracity. It takes all of the heavy lifting out of what can often be a problematic part of data validation, and takes it a step further than most. Not only does email veracity remove the need for a convoluted regexp to match against the email string, but it will also check the domain records to ensure that the MX entry is currently accepting mail, wow! Add this to the code on your model definition: that single line does the domain check too! You can turn it off, apply a…

Read more about Reject junk email addresses with email veracity

Trap rails from sending mail via SMTP

Trap rails from sending mail via SMTP Matt Mower has released a handy little tool called Mailtrap. Basically what it does as act as a very simple SMTP server that accepts requests from ActionMailer, but writes them to file instead of emailing them on. Installation is very straight forward: And then start it up: Which will start the mailtrap app to listen on port 2525. Now all that is left to do is change the config in your rails app to connect to localhost:2525 in either /config/environment.rb or /config/environment/development.rb: …

Read more about Trap rails from sending mail via SMTP

Installing Ruby, Rails, and Rubygems

Getting your first ruby on rails stack up and running can be a bit of a chore, here's a quick guide on how to do it on a linux box but the instructions should be equally good for OS X users. Now depending on your OS, you've got a few options on how to get ruby up and running. The easiest way is to use the built-in package management system. This will give you the latest known stable version of ruby for your environment, and hopefully reduce or remove any potential conflicts with other installed…

Read more about Installing Ruby, Rails, and Rubygems