But then you get home and, after a few days, you take another look at your project. “What is all this stuff? I didn’t write that!” There are so many folders and files, none of which you explicitly sat down and created. What are they all for?
After my first Rails Girls, I had this overwhelming desire to do an audit and remove what appeared to be superfluous code. The result? Well, predictably, my application broke and I had no idea what I needed to do to make it work again! It seems that all this “extra stuff” was necessary, after all. The only way I could get a working application again was rebuild it from scratch and not touch it. Being afraid to play with your app for fear of breaking it does nothing to help your learning.
This article will help break down what all this stuff is for and how it fits in. What you see here is a high-level overview, click on the links to read more detailed articles on each topic.
What is Ruby on Rails?
Ruby is a programming language developed in the mid-1990s. It is a scripting language that is also object-oriented. Rails is a framework for building web applications. It is written in Ruby. When you create a new Rails application, the framework part of it provides you with certain core functionality that is common to all web applications. It is this provided functionality that makes Rails awesome. And also makes Rails confusing for people who are learning. :-)
Along with the built-in functionality provided for you by the Rails framework, there is a library of Ruby gems that you can add to enhance your project. A gem provides instant functionality and reduces the amount of code that you need to write. A bonus to using gems is that they’ve been tested by developers in many live applications. This means that you can exclude them from your automated test scripts. Trust me, one less thing to test is always a good thing!
Rails uses two files to manage gems:
Gemfile, which is a list of all the gems to be installed, and
Gemfile.lock, which is a list of all the gems, together with their version, that have been installed in your application. See Gems and Bundler for more detail on this topic.
Configuration files allow you to customise the behaviour of your app. You will find them in the
config directory of your Rails app.
See Rails Configuration Files for more detail on this topic.
REST, MVC and Rails Routing
Your Rails project is a REST-based web application that follows an MVC architecture. All those buzz-words might sound confusing but, don’t worry, you will become familiar with them over time. Rails achieves this with the following three folders:
app/controllersand the file
When you typed
rails generate scaffold Post (or
Idea) in your Installfest (or Rails Girls) tutorial, you generated files that went into the models, the views, and the controllers folders. You also added a line to the
routes.rb file so that your posts (or ideas) could be accessed via a URL in your browser.
See REST and Rails for more details on how Rails implements REST.
See MVC and Rails for more details on how Rails implements MVC (Model View Controller).
See Rails Routing for more details on Rails Routing.
Databases in Rails
Most web applications have a database attached to them.
Rails comes with SQLite database by default, and this is the database that we most commonly use in our development environment. When it comes to production, however, most developers will use something like PostgreSQL (arguably the most popular) or MySQL.
Schema definition (DDL) is done through the use of Migrations. These migration files can be found in the
db/migrate folder. Data manipulation (DML) is done in Ruby through our Model.
See Database Migrations for more information on Migrations.
See MVC: Model for more information on communicating with our database through the Model layer.
Rails uses a framework called the Asset Pipeline to compress these asset files. It is outside the scope of this article so it will not be covered here. If you are curious, please see Asset Pipeline for more details.
Other Useful Resources
This article is focussed on Ruby and Rails. However there are other concepts that you will need to understand to become a well-rounded Rails developer. Below are some recommended resources for you to check out:
- An introduction to Object-Oriented Programming (OOP): object-oriented programming
- Instance variables and scope: Scope Wars
- A high-level introduction to git: The Big Picture of Git
- A detailed step-by-step guide to getting started with RSpec: RSpec Testing for Beginners, Part 1
That is your Rails project in a nutshell. This is intended to be a living document, and will be updated based on feedback from you. If you have any questions or comments, please comment below or email me.