How To Build A Web App, part 3 of ?: Ruby on Rails
This is the third in a series of articles taking you through all the actual steps in building a web app. If you’re an aspiring developer, if mucking around with teensy beginner tutorials frustrates you, if you’d love to build a properly substantial app that does fab things, these articles are for you.
We’ve just gone over a high-level, non-techie description of the app we’ll build. The time has come to launch the rad cosmic voyage of actually building the thing. Ready?
Any architect will tell you that the most important bit of any structure is the floor. The foundation. The foundations of any web app are its server. Today we’ll set up a web server running Ruby on Rails.
But what’s a web server, exactly? Glad you asked! If you’re already grounded on that, you can skip ahead to the paragraph beginning with “Now! Ruby, Rails.” But if you’re not totally sure, then I’ll give you some context, a foundation, for the web app construction ahead.
I’ll kick off with a few core concepts about how the Internet works. There’s a Thing called the Request-Response cycle. It’s the core concept for just about all internet traffic, and web servers are its keystone.
Request-Response
Consider an action we all do daily: type in a web address and hit Enter.
The browser fires off a web request at that destination URL; the request scurries across the internet to the computer at the other end. That computer Does Stuff. It fires back across the internet some response data (often a web page); your browser receives it; and Does More Stuff with it. You’d just typed in a web address, so your browser’s Does-More-Stuff action, this time, is display and render that address’s web page HTML.
And that’s all a web server is, really. It’s a computer running on the far side of the internet that yours connects to. It receives your Requests, and fires back its Responses.
Now! Ruby, Rails. What in tarnation are they?
Ruby, Rails
Recap Two. Same as before: if you feel solid about frameworks, skip ahead to “There are a zillion fantastic tutorials…”
Web servers generally don’t handle anything alone. They usually relay incoming requests to other, special-purpose programs. Like web frameworks. It’s these that do the actual processing and computing and number-crunching. for whatever apps you’re looking at. They’re neat. I’ll show you how to install one.
Ruby is a general-purpose programming language. In it, you can write just about any program.
Ruby on Rails, or just Rails (I’ll use Rails from now on, it’s easier), is a collection of programs and libraries, written in Ruby, designed specifically to ease the writing of web apps. There are tons of different ones around, though Rails is one of the more mature, venerable ones. If you want to remind me how Wrong I am about that in the comments, please do! Bring it on, baby.
Rails is solid. Rails is sound. Rails is mater. Rails is pater. Rails is eternal. When the Sun shrinks and the oceans wither and all is desolation and decay, Rails will remain. We’ll use Rails for our web framework.
There are a zillion fantastic tutorials already around on how to install both Ruby and Rails. I won’t waste words ripping them off. I’ll provide links to a few shortly. But first, some precious context.
You know when you install a modern PC/console game? They’re tens of gigabytes. Takes ages. But all the UI ever shows of the install process’s technical details is a progress bar, plus possibly a line of text giving an incredibly brief summary of the installation process. What’s it actually doing in there? What impenetrable, arcane convolutions does the install-UI conceal?
Well, young padawan, today’s your lucky day! The time has come to reveal the actual guts of installing something complicated.
Here’s one for Macs, and here’s one for Linux. These install the Rails core libraries themselves, which you then use to create as many Rails-based web apps as you please.
Using Windows? Oh dear. Installing Rails can technically be done there, but I wouldn’t advise it. That said, here’s an article discussing it. Peruse at your leisure.
I do acknowledge, by the way, that each install guide is fairly hefty. If you’ve never tried installing Rails on your own computer, you may want to cordon off an afternoon or two. But totally worth it. Follow either of those links in graphic detail and you will learn more about computers and programming than months at school. And please don’t be intimidated by that! I say that in the knowledge that me saying “please don’t be intimidated” won’t make any trepidation of yours somehow magically go away. But pushing boundaries is what web dev is all about. If you follow the instructions in either tutorial and hit a roadblock you can’t overcome, let me know in the comments and I’ll help all I can.
All done? Fab! Onward.
Now we set up a new Rails API. If you wish to learn more about exactly what that is, check this out from the Rails website’s own docs. You really don’t have to right this second, I’ve just included it for completeness and curiosity.
Here’s the actual command. Hop back onto your command line, navigate to a spot on your file system where you’d like to set up new apps, and run this:
$ rails new gigs --api
You’ll see a huge blizzard of command-line feedback, showing a ton of new files created. Here’s what my own command line showed:
$ rails new gigs --api
create
create README.md
create Rakefile
create .ruby-version
create config.ru
create .gitignore
create Gemfile
run git init from "."
Initialized empty Git repository in /Users/michaelclarke/Work/gigs/.git/
create app
create app/assets/config/manifest.js
create app/assets/javascripts/application.js
create app/assets/javascripts/cable.js
create app/assets/stylesheets/application.css
create app/channels/application_cable/channel.rb
create app/channels/application_cable/connection.rb
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create app/jobs/application_job.rb
create app/mailers/application_mailer.rb
create app/models/application_record.rb
create app/views/layouts/application.html.erb
create app/views/layouts/mailer.html.erb
create app/views/layouts/mailer.text.erb
create app/assets/images/.keep
create app/assets/javascripts/channels
create app/assets/javascripts/channels/.keep
create app/controllers/concerns/.keep
create app/models/concerns/.keep
create bin
create bin/bundle
create bin/rails
create bin/rake
create bin/setup
create bin/update
create bin/yarn
create config
create config/routes.rb
create config/application.rb
create config/environment.rb
create config/cable.yml
create config/puma.rb
create config/spring.rb
create config/storage.yml
create config/environments
create config/environments/development.rb
create config/environments/production.rb
create config/environments/test.rb
create config/initializers
create config/initializers/application_controller_renderer.rb
create config/initializers/assets.rb
create config/initializers/backtrace_silencers.rb
create config/initializers/content_security_policy.rb
create config/initializers/cookies_serializer.rb
create config/initializers/cors.rb
create config/initializers/filter_parameter_logging.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/new_framework_defaults_5_2.rb
create config/initializers/wrap_parameters.rb
create config/locales
create config/locales/en.yml
create config/master.key
append .gitignore
create config/boot.rb
create config/database.yml
create db
create db/seeds.rb
create lib
create lib/tasks
create lib/tasks/.keep
create lib/assets
create lib/assets/.keep
create log
create log/.keep
create public
create public/404.html
create public/422.html
create public/500.html
create public/apple-touch-icon-precomposed.png
create public/apple-touch-icon.png
create public/favicon.ico
create public/robots.txt
create tmp
create tmp/.keep
create tmp/cache
create tmp/cache/assets
create vendor
create vendor/.keep
create test/fixtures
create test/fixtures/.keep
create test/fixtures/files
create test/fixtures/files/.keep
create test/controllers
create test/controllers/.keep
create test/mailers
create test/mailers/.keep
create test/models
create test/models/.keep
create test/helpers
create test/helpers/.keep
create test/integration
create test/integration/.keep
create test/test_helper.rb
create storage
create storage/.keep
create tmp/storage
create tmp/storage/.keep
remove app/assets
remove lib/assets
remove tmp/cache/assets
remove app/helpers
remove test/helpers
remove app/views/layouts/application.html.erb
remove public/404.html
remove public/422.html
remove public/500.html
remove public/apple-touch-icon-precomposed.png
remove public/apple-touch-icon.png
remove public/favicon.ico
remove app/assets/javascripts
remove config/initializers/assets.rb
remove config/initializers/cookies_serializer.rb
remove config/initializers/content_security_policy.rb
remove config/initializers/new_framework_defaults_5_2.rb
remove bin/yarn
run bundle install
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/..................
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies....
Using rake 12.3.3
Using concurrent-ruby 1.1.5
Using i18n 1.6.0
Using minitest 5.11.3
Using thread_safe 0.3.6
Using tzinfo 1.2.5
Using activesupport 5.2.3
Using builder 3.2.3
Using erubi 1.8.0
Using mini_portile2 2.4.0
Using nokogiri 1.10.4
Using rails-dom-testing 2.0.3
Using crass 1.0.4
Using loofah 2.2.3
Using rails-html-sanitizer 1.2.0
Using actionview 5.2.3
Using rack 2.0.7
Using rack-test 1.1.0
Using actionpack 5.2.3
Using nio4r 2.4.0
Using websocket-extensions 0.1.4
Using websocket-driver 0.7.1
Using actioncable 5.2.3
Using globalid 0.4.2
Using activejob 5.2.3
Using mini_mime 1.0.2
Using mail 2.7.1
Using actionmailer 5.2.3
Using activemodel 5.2.3
Using arel 9.0.0
Using activerecord 5.2.3
Using mimemagic 0.3.3
Using marcel 0.3.3
Using activestorage 5.2.3
Using msgpack 1.3.1
Using bootsnap 1.4.4
Using bundler 1.16.4
Using byebug 11.0.1
Using ffi 1.11.1
Using rb-fsevent 0.10.3
Using rb-inotify 0.10.0
Using ruby_dep 1.5.0
Using listen 3.1.5
Using method_source 0.9.2
Using puma 3.12.1
Using thor 0.20.3
Using railties 5.2.3
Using sprockets 3.7.2
Using sprockets-rails 3.2.1
Using rails 5.2.3
Using spring 2.1.0
Using spring-watcher-listen 2.0.1
Using sqlite3 1.4.1
Bundle complete! 9 Gemfile dependencies, 53 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
run bundle exec spring binstub --all
* bin/rake: Spring inserted
* bin/rails: Spring inserted
You’ll see there are two main sections. The command creates a great big list of new files; and it installs a list of new gems. You may remember a “gem” from installing Rails: it’s a program or library, written in Ruby, and publicly available on rubygems.org.
There are millions of the beasties. Rails needs a few dozen to do its Rails-flavoured stuff. Rails itself is a gem. I’ll take you through gems in more detail in other articles.
And that’s this article complete! You’ve created a new Rails app. Right now it doesn’t actually do a thing, but we’ll soon fix that.
In our next dozen-odd articles, I’ll take you through what all these text files are, and what they do, and how they do it. I’ll explain what a Rails API is, and how to build one.
Next time: version control, Git.