The first program that you’ll see in many programming related tutorials is called hello world. A kind of here i am of the programming world, and in good tradition, we’ll go through how to do that with rails now. If you haven’t read the previous article on choosing a framework, i suggest you do that first.
So the aim is that by the end of the article you will have written a web application with exactly one page, and that page will have the text “Hello World” on it. This may not seem like much, but that is looking at it from a users perspective. From a developers perspective we will need to use, and at least partly understand, our framework, and that is quite a lot.
There are so many combination of machines and operating systems, and so many good tutorials on how to install rails, that you’ll just have to google it. For mac, gorails is nice, for windows the railsinstaller is better, tutorialpoint for linux, and installrails is good for all, if a bit extensive for us at this point.
At the end you will need a ruby version newer than 2.2, and a rails version of 5.0 or higher. You can check the ruby version by typing
> ruby -v
into the terminal. Also rails accepts a version option, so
> rails -v
should return something over 5.0. If tutorials talk about installing git or postgres, you can skip that for now. Also some prefer to use
rvm as a ruby manager, while i usually use
Packages, gems and bundler
In ruby a package is called a gem (because ruby is a gemstone), in other languages these are called library, sometimes module. Gems have a name, version number, author etc, and contain the actual ruby code. Gems are listed (and can be searched) on the official gem site rubygems.org , or others, like rubylaser.org . Some gems, like rails, also have executables (scripts) included. This means, once you have installed the rails gem, you can type rails in the terminal.
Gem is itself a gem and an executable, which comes preinstalled with every ruby installation. This means you can type
(or gem -h) in your terminal and it will tell you a bit about itself and how to use it.
> gem -v
will give you the gem version number for example and “gem install ..” and “gem uninstall ..” are main commands you will use. Rails is a also gem and as such you can install it with
> gem install rails
Rails also includes an executable script, aptly called rails, so you can type
rails and get some information about creating a new rails application.
For historical reasons there is one more gem/command that we need to know: The gem is caller bundler, but the command is bundle (noun/verb kind of thing). This collects all the dependencies of an application and installs them in a repeatable manner, so all developers always have the exact same gems and versions of gems. Not having the same gems has before led to lots of confusion and frustration, so bundler was built much later than gem. Nowadays it is recognised that the functionality is very similar and the two tools are in the process of being unified.
From the rails installation you should have gotten a bundler, so typing
> bundle -v
should give you a version number. But with bundler you have to use the -h to get help, like
bundle -h , as
bundle is the same as
bundle install, which is not useful yet.
Creating an application
Additionally to being a framework, rails can also generate code for us. It can be used to generate different parts of an application, which we will go into below, but also a whole new application. We will use this in a second.
Before though, we have to understand an import philosophy of rails, which is summarised as convention over configuration. This means that for many things, rails will use preset defaults, ways of doing things. Rather than us having to tell it about things (configure them), it will make assumptions according to the “conventions” (though we can still configure if we want).
In this first encounter with rails, this means when rails generates a skeleton for our app, all the “things” are where they “should” be. This should can be seen as the rails way, and it is this that gives use the “frame” that we work in. Ok, so let’s start. In your user directory, just type (into the terminal)
rails new hello_world, and this will generate the “HelloWorld” application in a directory called hello_world. You will see a fair bit of output, first about the files and directories it creates, then about the gems it uses.
We will go over the main parts of the application in the next post. For now just go into the new directory (
cd hello_world) and open the directory in atom (
atom .). Look around, read the Readme, if you want jump ahead to the getting started guide and read about the directory structure.
Instead of more theory, we will jump straight in and create the parts that we need to get our greeting done. All the rest of the commands will be inside the hello_world directory, and the first is this
> rails generate controller Hello world
This will generate what is called a controller, one action and a view. The controller is called HelloController (by convention) and is defined inside the file
app/controllers/hello_controller.rb. The action, which is a method on the controller, is the last argument, “world”. The view is also called world, but defined in it’s own file,
app/views/hello/world.html.erb . As the ending suggests, this is an html file, and the “.erb” means that we can use erb (embedded ruby) in this file.
There are other things created some of which we will only learn about later, some are even removed from the screenshot, so as to focus on the important. The main missing element, called a route, is defined in the file
config/routes.rb and looks like this:
The route above is saying, that if the app receives a
/hello/world url, then it will route that to the
HelloWorld controller and the world action in it. Rails convention goes further to stipulate that after that code has run (the world action here), a view of the same base-name will be rendered, ie
world.html.erb. The default place for the view, is in the
app/views directory, inside a directory named after the controller, ie in this case
app/views/hello . Don’t worry if you didn’t get all that at first go. This is the core of rails, and you will repeat it many times, until it fills your dreams :-) In fact the whole next post will be about it.
We can see it in acton though. Just type
> rails server
into your terminal and we see the app starting up. It tells us that we can see the app on our machine, at an address called localhost (a generic name for your machine) and port 3000. Tcp actually has 65k ports, but the first 1000 are reserved, so we use a higher one.
To see our greeting, click http://localhost:3000/hello/world . If you want your greeting without the autogenerated # mark, or the extra text, you can go and open the file it says, namely
app/views/hello/world.html.erb , and edit it.
Let’s just go over it one more time to get used to our new vocabulary.
- We have installed the ruby language (version 2.2 or greater).
- With it came a package manger called gem, also we call the packages gems.
- We installed a gem called rails. Rails is our application framework.
- With rails came many gems, and especially one called bundler, which manages them all.
- We used rails to generate a skeleton application hello_world. This is a a whole bunch of different files in many directories that work together as a whole.
- We used rails to generate a controller, action and view. Basically a ruby class and an html file with certain naming conventions.
- We started our server and got a response for a certain url
When we accessed the localhost:3000/hello/world url, there was output on the terminal similar to above. This is called a log, and can be used to see what the system did at any time (see the timestamps).
Here we just see the same things we already went through: A request for “/hello/world” is received and processed by the HelloController. A view “hello/world.html.erb” is rendered, inside a layout which we have not learned about yet. We can also see that it didn’t take very long, 30ms is less than we can perceive.
So, in just under 1500 words, a hello world web application. Not bad. Next ı’ll explain a bit more the principles behind what just happened, and start involving a database too.