1. Computing

Creating a URL Shortener in Rails

By

Guiding You Through the Process

There are many URL shortening services out there. Popular ones include TinyURL.com and Bit.ly. In the age of twitter, text messages and micro-communications, long URLs just aren't always a good fit. These services allow people to more readily share links to content on your site, and that's never a bad thing. And while there's not much room for yet another standalone URL shortening service (we've seen so many of them come and go over the years, and when they die all the links they shortened are now dead), adding this type of service to your web applications makes it easier for people to share links to your content. Everyone wins!

This tutorial is somewhat unorthodox. The traditional RESTful resources used in most Rails applications are not used here. Since the application is just so small, they're just not feasible. However, since the scaffold generator is so handy, it's still used to bootstrap the project. Instead, what you'll find here is a bit of insight into the Ruby on Rails router, something you don't usually touch manually when using RESTful resources.

If you prefer not to follow along, just want to see the final result or something isn't working, the source code is always available on Github. You can also see this application deployed on the Heroku cloud here.

  1. Brainstorming - Before we jump in, let's think about what we're doing here. What is a URL shortener, what does it do, what is beyond the scope of the project and, perhaps most importantly, what data will it need? Even though Ruby on Rails is all about rapid web application development, it's not that rapid, you should still sit down and think about what you need before writing any code.
  2. Bootstrapping - Now that we know what we're going to need, let's block out the initial database. We'll use the scaffold generator here, even though that's really intended for RESTful resources. We'll just rip out the parts we don't need later, we use it since it generates 90% of what we need and that's too convenient to ignore.
  3. Implementing - The database is all set after bootstrapping our project, now it's time to actually make this into a URL shortening service rather than just a blank scaffolded project. This is where we get our hands a bit dirty (not too dirty, this is a tiny project) with the Rails router, fixing up the model and the views, etc. At the end of this step, it'll be fully functional (but ugly).
  4. Styling - Now that the URL shortening application works, it's time to make it look good. This article won't cover CSS styles in general, or any kind of web design or HTML, but rather how to integrate such things into your Rails projects.
  5. Deploying to Heroku - Now that the application is finished, more or less, it's time to deploy it. There are a lot of options for deploying a web application. You can get a dedicated virtual machine, you can buy a monthly hosting plan, but those are a bit too traditional. Instead, you can deploy your application to the cloud. You can deploy your application to Heroku. Not only is it very simple to do, it's also free (at least for small web applications).

And there you have it, a URL shortener in Rails. If you'd like to know more about Rails, there are plenty more articles here you can read and of course, many many more about Ruby in general. If you're new to both Ruby and Rails, it's a good idea to get a solid foundation in Ruby before delving too deeply into Rails. Many problems Rails newbies don't have anything to do with Rails, but a misunderstanding of how Ruby works.

  1. About.com
  2. Computing
  3. Ruby
  4. Ruby on Rails
  5. Ruby on Rails 3 Tutorials
  6. URL Shortener in Rails 3
  7. Creating a URL Shortener in Rails

©2014 About.com. All rights reserved.