1. Computing

An Introduction to Rails: a Quick Blog Part 1

By

An Introduction to Rails: a Quick Blog Part 1
An Introduction to Rails: a Quick Blog Part 1

If you were around in 2005 or so, you may have seen the demo DHH (the creator of Ruby on Rails) gave creating a blog in 15 minutes (where he keeps saying "woops!" whenever Rails does something neat). While a blog is not a terribly useful Ruby on Rails application, it is an excellent "hello world" type application for Ruby on Rails. At its core it just grabs things from the database and shuttles them to the view which outputs HTML. So let's dive into Rails and make a quick blog.

Most of what you see here is "the quick way." While nothing is "wrong," a lot of steps are skipped. There are no tests, for example, and no version control is used. There's also no CSS or images and the HTML is minimal, so there's not much about the frontend in this article. It should, however, orient you in the Rails framework and give you a bit of hands on with the various components.

Installing Rails

This used to be much more difficult than it is now. To install Rails, all you really need is Ruby and a database, most likely the SQLite database. You used to need to set up MySQL, which was a whole other can of worms but as SQLite is just fine for development, you can skip all that for now. There are also installers that can help you immensely.

For Windows and Linux, I recommend using Rails Installer. It's a one-click installer for Ruby, Ruby on Rails, SQLite and a few other things. If you're just checking out Rails, this will get the job of installing Rails done quickly, if you need a different configuration later, worry about it then. For Ubuntu Linux, the process is not very complicated. You can sudo apt-get install ruby-full rubygems build-essential libsqlite3-dev, and this will get you most of the way, then you can sudo gem install rails.

Generate the Project

Rails will generate an empty project for you. There are a ton of configuration files, stub files and a directory structure you'll need that would just be difficult to create by hand, you'd never be able to remember it all. So, once you have Rails installed you should have a rails command in your path. So fire up your terminal program, change to the directory you want your project directory to live and run the command rails new blog. It'll rattle off a bunch of stuff, telling you all the files it created, but don't pay much attention to that quite yet.

Fire Up Your Editor

Time to start editing files (almost). Any editor will do really, but it should have, at the minimum, a file tree view on the left to easily switch between files (Rails projects are spread over a large number of files), syntax highlighting and automatic indentation. I'm a Vim user, but you can use emacs or, if you want an editor that's easier to learn how to use, you can use anything from gedit (a minimal text editor that comes with most Linux distros) to Notepad++ (a good free editor on Windows) or TextMate (an editor on OS X in wide use in the Rails community).

Once you have your editor fired up, take a look at the files in your project directory. The directory you're most interested in is the app directory, so open that up and we'll look at it. Inside that directory, there are three directories you'll mostly be interested in right now, and they'll be pretty empty for the time being. These are controllers, models and views. As Rails is an MVC framework, this is where the meat of the application programming takes place.

MVC Frameworks

I just mentioned "MVC framework," what is that? MVC stands for model, view, controller, and is a way of segmenting your application, separating your data from your view, and your logic from your view. This is a way of getting away from "PHPitis," where all your pages are giant HTML files with PHP mixed in, and you can never find anything (I know better code can be written with PHP, but you know the type of code I'm talking about). The three components of an MVC framework are as follows.

  • Model - The model is responsible for the data. Behind the scene, the model (the code that takes care of this is called ActiveRecord) will handle the database, but your code sits at a higher level. Here you'll ensure the integrity of your data. You'll ensure that no users are created without usernames, that passwords are hashed, that no two blog entries share the same ID, and that everything will be smooth sailing when the controller retrieves data using the model.
  • Controller - The controller is the thinnest part of most applications, holding the least amount of code. It's responsible for accepting the HTTP query from the HTTP server, figuring out which controller that request belongs to and routing it to the correct action. It's then responsible, mostly, for retrieving data using the model and giving that data to the view. Your actions are often just a few lines long.
  • View - The view is responsible for taking the data given by the controller and generating (in most cases) HTML output. Your view code is mostly HTML code with a tiny amount of Ruby mixed in (using a templating language called ERB, or there are others like Haml, should you choose). The objective here is to put as little code in the view as possible, as code mixed in with HTML often gets forgotten about and the output is heavily coupled with the inner workings of the application. This is one of the major motivations behind using an MVC framework, keeping code out of the view.

Creating the Database and Starting the Server

It's time to actually create the database. By default, Rails will configure an SQLite 3 database. This database requires no server (as MySQL and Postgres do), but the actual database file must be created. To do this, you run a rake task called db:create by running (big surprise) rake db:create. You may get an error message about a Javascript runtime not being available, so take a sidestep to fix this. Add the line gem 'therubyracer', :require => 'v8' to the end of the Gemfile file in your project directory and run bundle install. Once that's finished, run rake db:create again.

Time to start the server. By default, Rails is its own server, using a server like Webricks or mongrel, either built into Ruby or as a gem. So you won't need to configure Apache or anything at this point. In your project directory, just run rails server and it'll start a server on port 3000, so hop on over to http://localhost:3000/ and voila, your Rails application is up and running. OK, it doesn't do anything yet, so stay tuned for part 2, actually making the blog.

  1. About.com
  2. Computing
  3. Ruby
  4. Ruby on Rails
  5. Ruby on Rails 3 Tutorials
  6. An Introduction to Rails: a Quick Blog Part 1

©2014 About.com. All rights reserved.