#310 Getting Started with Rails
Ruby on Rails is an awesome Web framework but it can appear rather overwhelming when you look at it for the first time. This series often covers intermediate topics and it can be difficult to find a single location to point new Rails programmers at. In this episode we hope to resolve that.
If you’re running Windows the best way get Ruby and Rails installed is to use the RailsInstaller. This is a single package that will install everything you need to set up a Rails environment so you can skip the rest of this section.
If you’re running OS X it’s worth installing Homebrew before you install Rails as it makes it much easier to install some of the software you’ll need. It only takes one command to install this, which you can find on the installation page. Once you’ve installed Homebrew you can use it to install a couple of useful packages, namely
git for source control and
sqlite as a database. You’ll need these to set everything else up and you can install them by running this command.
$ brew install git sqlite
If you’re running Linux you can install Sqlite and Git through your distribution’s package manager.
Next you’ll need to install the latest version of Ruby. If you’re running OS X or Linux this is best done with RVM. This can also be installed with a single terminal command that you’ll find on RVM’s home page and once it has installed you can install any version of Ruby you like through the
rvm command. To install the latest version of Ruby, currently 1.9.3, you can run
$ rvm install 1.9.3
This will download and compile Ruby 1.9.3 and when it has finished we can use that version by running
$ rvm use 1.9.3 --default
--default option will make this version the default for your system.
If you’re more UNIX-savvy there’s an alternative to RVM called rbenv. This takes more work to set up but it’s a lighter approach to installing multiple versions of Ruby.
You can now run the
ruby command to make sure that you have the correct version installed and set as the default version.
$ ruby -v ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]
If you have then you can install Rails by running
$ gem install rails
This command will install the latest version of Rails, currently 3.1.3. If you want to install a specific version you can use the
$ gem install rails --version 2.1
It’s important to install the correct version of Rails that matches the tutorial you’re following. If, for example, you have a book that was written for Rails 2.3 you should install that specific version. If you’re not sure which version of Rails your tutorial is using the Wikipedia page on Ruby on Rails has a table showing the release dates of the major versions so as long as you know when your tutorial was published you should be able to work out which version of Rails it was written for. Once you’ve installed Rails you can check what version you’re currently running with this command:
$ rails -v Rails 3.1.3
If you have to install multiple versions of Rails you need to bear in mind that some versions are incompatible with each other. To work around this you can use RVM gemsets to and install separate versions of Ruby each with its own Rails installation.
Once you have Rails installed and working it’s time to work through some tutorials the Ruby On Rails Guides are a good place to start is. This site has a nice Getting Started section which will show you how to create a simple blogging application and which will give you a good idea about what’s involved in creating a simple Rails application. If you find yourself feeling overwhelmed while working through a tutorial like this that’s perfectly understandable. There’s no need to expect to understand everything all at once and persistence is important here.
Rails For Zombies is another tutorial that’s worth working through. This is a fun interactive way to see what’s involved in building a Rails application. You should also take a look at The Ruby on Rails Tutorial. This covers almost every aspect of Rails and related subjects such as Ruby, Git and Heroku. You don’t need to tackle this all at once but it’s worth taking a look at.
Divide and Conquer
The Try Ruby website is a fun, interactive way to learn the basics of Ruby and don’t forget that you can also run Ruby code interactively in the terminal by running
irb. Inside a Rails application you can run
rails console to load that application’s environment. This is a great way to learn something and you use it to experiment with your application.
To hone your Ruby skills further you can take various challenges. Ruby Quiz is an oldie-but-goodie. It has a diverse archive of challenges and you can pick one you like and try coding a solution for it. There are also learning challenges on the Ruby Learning Blog with a list on the home page of all of them. For more recent but also more difficult challenges take a look at Codebrawl. This has some fun ideas that you can try our to improve your skills. Finally you should try Ryan Bates’ Ruby Warrior project. This is a fun game that will help you learn Ruby.
Your Own App
Going through tutorials and challenges is great but it’s not as rewarding and motivating as creating a Rails application of your own. If you’ve got a great idea for an application where should you start? It’s a good idea to begin with the user interface. Sketch up each of the pages in your app using either pen and paper or digitally with a program like Balsamiq Mockups. Doing this will help you to divide your application up into resources. For example a form will usually be used for creating or editing a resource and other pages will list resources or show a single resource. You can then create a model and controller for each of these resources and you’ll be well on your way to creating your Rails application.
As you’re building your application you’ll run into areas that aren’t covered by the tutorials you’ve done. This is where RailsCasts comes in. It’s a great reference for adding various features to your application such as authentication, administration and much more. Source code is included for most of the episodes and you can download it and try running the applications for yourself. More recent episodes include before and after versions of the project so that you can follow along.
The Ruby Toolbox is another great resource. It provides a nice list of gems for accomplishing various tasks in Ruby and Rails. Make sure to look at the rest of the Rails Guides, too. There are a lot of great topics covered here that will help you hone your skills in various parts of Ruby and Rails. Finally don’t forget the Ruby on Rails API site. This is the place to go for finding documentation on any specific method in Rails.
As you work on your applications you’re bound to run into problems that you can’t solve on your own. In these situations you can turn to the Ruby and Rails communities. A good place to start is the Rails Talk mailing list. You can search the archive to see if anyone has a similar problem and, if not, ask a question there. StackOverflow is another great site for asking programming-related questions but don’t forget to tag your question with ruby-on-rails.