Rails for beginners

This guide covers installing and running Ruby on Rails.

After reading it, you will know:

How to install Rails, create a new Rails application, and connect your application to a database.

The general structure of a Rails application.

Basic principles of MVC (Model, View Controller – “Model-View-Controller”) and design based on RESTful.

How to quickly generate initial Rails application code.

Assumptions in this guide

This guide is for beginners who want to start a Rails application from scratch. It does not assume that you have worked with Rails before.

Rails is a web development framework written in the Ruby programming language. If you don’t have Ruby experience, you might find it hard to get started learning Rails right away. There are several good English-language resources dedicated to learning Ruby, for example:

Official website of the Ruby programming language

List of Free Programming Books

Note that some of the resources, while still great, cover older versions of Ruby like 1.6 and 1.8 in particular, and don’t include some of the syntax you’ll see in everyday Rails development.

What is Rails?

Rails is a web development framework written in the Ruby programming language. It is designed to make web application programming easier by making a number of assumptions about what each developer needs to create a new project. It allows you to write less code while programming compared to other languages ​​and frameworks. Professional developers on Rails also note that it makes developing web applications more fun =)

Rails is a masterful piece of software. It makes the assumption that there is a “best” way to do something, and is designed to encourage that way – and in some cases even discourage alternatives. If you study “The Rails Way”, you may discover a significant increase in productivity in yourself. If you persist and bring old habits from other languages ​​into Rails development and try to use patterns learned elsewhere, your development experience will be less happy.

The Rails philosophy includes two important guiding principles:

Don’t Repeat Yourself: DRY is a software engineering principle that states that “Each piece of information should have a single, non-redundant, authoritative representation in the system.” Don’t write the same information over and over again, the code will be easier to maintain and more extensible and less buggy.

Convention Over Configuration: Rails has opinions on the best way to do a lot of things in a web application, and defaults to these conventions instead of forcing you to fine-tune numerous configuration files.

Creating a New Rails Project

The best way to use this guide is to go through it step by step. All steps are essential to running the example application, and no additional code or steps are required.

By following this tutorial, you will create a Rails project called blog, a very simple web blog. Before we start building the application, we need to make sure that Rails itself is installed.

TIP: The following examples use $ to denote a terminal input string on UNIX-like operating systems, although yours may be configured differently. If you’re on Windows, your string will look something like c:\source_code>

Installing Rails

Before installing Rails, you need to make sure that you have the required prerequisites installed on your system. These include Ruby and SQLite3.

Open command line applications. On macOS open Terminal.app, on Windows select “Run” from the Start menu and type ‘cmd.exe’. Any commands that begin with a dollar sign $ must be run on the command line. Make sure you have the current version of Ruby installed:

Rails requires Ruby version 2.2.2 or later to be installed. If the version number is less than this, you will need to install a fresh copy of Ruby.

TIP: There are a number of tools to help you quickly install Ruby and Ruby on Rails on your system. Windows users can use the Rails Installer and macOS users can use Tokaido. More installation methods for most operating systems can be seen at ruby-lang.org.

If you’re on Windows, you’ll need to install the Ruby Installer Development Kit.

You will also need to install the SQLite3 database.

Many popular UNIX-like operating systems ship with an acceptable version of SQLite3. On Windows, if you installed Rails using the Rails Installer, you already have SQLite installed. Other users can refer to the installation instructions on the SQLite3 website. Check that it is correctly installed and in your PATH:

$ sqlite3 –version

The program should report its version.

To install Rails, use the gem install command provided by RubyGems:

$ gem install rails

To check that everything is installed correctly, you need to do the following:

$ rails –version

If something like “Rails 5.1.1” is displayed, you can continue.

Creating a Blog App

Rails comes with a number of scripts, called generators, designed to make a developer’s life easier by creating everything they need to get started on a particular task. One of them is the new application generator, which provides you with the foundation of a Rails application so you don’t have to write it yourself.

To use this generator, open a terminal, navigate to a folder where you have permission to create files, and write:

$ rails new blog

This will create a Rails app called Blog in the blog directory and install the gems whose dependencies are mentioned in the Gemfile when using bundle install.

NOTE: When using the Windows Subsystem for Linux, there are some restrictions on filesystem messages, meaning that the spring and listen gems must be disabled, which can be done by running rails new blog –skip-spring –skip-listen.

TIP: You can see all the possible command line options that the Rails application builder accepts by running rails new -h.

Once you’ve created the blog app, navigate to its folder:

$ cd blog

The blog directory contains several auto-generated files and folders that define the structure of the Rails application. Most of the work in this tutorial will take place in the app folder, but for now, let’s go over the functions of each folder that Rails creates in the new default application:

Leave a Reply

Your email address will not be published. Required fields are marked *