For years I have been building applications using C#. In recent years I have taken a keen interest in Ruby and Rails. I thought that I would blog about how to get started, to make it easier for me to remember. Hopefully you get a kick out of it.
Ruby on Rails emphasises the use of well-known software engineering patterns and principles, such as active record pattern, convention over configuration, don't repeat yourself and model-view-controller.
Active Record Pattern
Convention Over Configuration
Don't Repeat Yourself
- A controller can send commands to its associated view to
change the view's presentation of the model (e.g., by scrolling
through a document). It can also send commands to the model to
update the model's state (e.g., editing a document).
- A model notifies its associated views and controllers when
there has been a change in its state. This notification allows the
views to produce updated output, and the controllers to change the
available set of commands. A passive implementation of MVC omits
these notifications, because the application does not require them
or the software platform does not support them.
- A view requests from the model the information that it needs to generate an output representation.
To get started is very easy. Perform the following steps:
- Install rails by running gem install rail.
- Run rails new <application_name> -T. The -T option means [--skip-test-unit] # Skip Test::Unit files
- Go to the application that you created cd <application_name
- Edit your Gemfile and add the following to the file:
group :development do
- Since I don't like plain old HTML I prefer to use Slim. To make sure rails uses this template engine add the following to your Gemfile gem "slim-rails"
- Make sure now you run bundle install to make sure your new gem is up
- To get RSpec run the following command rails generate rspec:install
- Run rails server and go to http://localhost:3000/ to make sure everything is ruinning
- Once you are happy lets get it in version control:
- Run git init .
- Run git add -A
- Run git commit -m "Initial commit”
How easy was that.
- rails generate scaffold Post name:string title:string
content:text. This is such a
powerful command as it goes and creates a new model, view,
controller, specs and a db migration.
- To make sure we create a the
data in the database lets run the migration run
- Lets make sure that it all works
by running rails server
- Go to http://127.0.0.1:3000/posts and play around with it
As anyone that knows me knows I am a huge advocate of Behaviour Driven Development. The thing I love about ruby is that it makes it so easy. So let's write a test. Open up post_spec.rb and add this test
it 'should not save post without title' do
post = Post.new
To implement the solution we will add the following to the Post model and add
validates :title, :presence => true
Pretty easy, for more information please look at this excellent guide on testing.
Configuration in rails is done through code and using YAML files. In your rails application all configuration is stored in the config folder. The most common configuration that you will deal with is:
- config/application.rb – Contains all the application
configuration. Rather than putting everything in this file it is
recommended to add all of your changes to config/initializers
- config/environments/* - Contains environment specific configuration. This configuration takes precedence to the application configuration.
Rails abstracts the database nicely by introducing us to a concept of migrations. Think of it as a way to version your database. When you generate a model by running:
rails generate model Product name:string description:text
Rails will create a file under the db/migrations folder. This migration will look like:
class CreateProducts < ActiveRecord::Migration
create_table :products do |t|
This is just a template and it is encouraged that you change it. As you should be thinking about the queries that you will be making against this model.
More information on migrations can be found here.
Another cool aspect is that this pipeline allows you to chain different technologies together. For example if you wanted to use CoffeeScript you would name your file something.js.coffee and include the coffee-rails gem, now the asset pipeline will allow you to write all of you front end in CoffeScript. How cool is that?
For more information I recommend reading the official info on the asset pipeline.
This is just a basic introduction to the platform. As you can see Ruby on Rails is a fantastic platform. However realise that it is very opinionated, which is not a bad thing. What I love about rails is how easy it is to get going and testing is not an after thought. There is a lot more to rails which I will explore by writing an application that allows me to explore the concepts of lean projects.
To learn more about rails I recommend looking into the online guides and read the best book on the subject Agile Web Development with Rails (4th edition).