How To Code A Ruby on Rails Web Application

Rails is the framework, Ruby is the language.

Designed by David Heinemeier Hansson in 2005, it’s become renowned in the Internet startup world for its adoption by some of the leading “startups” of our time, including Stripe, Uber and Groupon.

If you want to learn to program in Ruby on Rails, this tutorial should give you an overview of what to do. I won’t go into specifics because I just want to give you an idea as to the structure of an application. Kampanye di media sosial If you follow what I propose, you should more fully understand how these applications work.

Web Applications

All software applications work in the same way –

 

  • Data is inputted
  • Data is processed
  • Data is outputted

The way the data is inputted and processed is dependent on the platform your application runs on. How it is outputted depends on your application. 

The difference with web applications is that their logic runs on a server, with the data IO being passed through the Internet (specifically, the HTTP protocol).

The complication of web apps is that you require the ability to accept inbound data, and return responses. This is handled by a web server program (NGinx or Apache). I’ll explain this in a minute.

Software Stack

When you create a piece of software, you have to consider the “stack” on which it runs.

The “stack” is all the software required to run your application. In the world of desktop games, for example, the “stack” may include the likes of DirectX or a particular graphics driver.

The main hold-back for would-be web application developers is understanding how the “web” software stack works. Web works similarly to native applications, except for one distinct difference – stateless.

The “Internet” operates under the HTTP protocol. By nature, this is known as a “stateless” protocol – each request you send is considered independent to the last. Unlike stateful protocols (which retain state), stateless protocols have to rebuild the application’s state each time.

Whilst this means nothing to most people, the point is that if you’re going to develop a web based application, you need to use a framework or technology set which makes the stateless nature of HTTP as integrated as possible. Most pertinently, you need an authentication system which rebuilds the user’s session on every request (I’ll explain this in a second).

Ruby vs PHP

Ruby (the language) is akin to PHP – they are both procedural and both are used heavily on the Internet.

The main difference between Ruby and PHP is that PHP is accessed directly on the client-side, Ruby needs a proxy.

Applications such as WordPress are built with PHP because it’s free, open source and can be run on any LAMP (Linux Apache MySQL PHP) server (which is basically all of the shared hosting in existence).

The point with Ruby is that it is a LOT more temperamental than PHP – it requires running processes to help it operate and can often fail to start if any issues arise.

Basics

To get started, you need three things:

 

  • An IDE (Integrated Development Environment)
  • A Ruby-Compatible Web Server (Heroku)
  • Ruby, Rails & GIT Installed On Your System

I’ll explain how it works. 

An “IDE” is a text editor with the ability to discern the code you input. I currently use Atom (free) from Github. You can download it from Atom.io.

The IDE allows you to write the code. Whilst you’re free to use a standard text editor (Notepad or Notepad++), it’s much better to use a system such as Atom or even Visual Studio, as to gain the full functionality of the language (linting etc).

From here, you’ll also need to install Ruby, Rails and GIT on your development system. Ruby is the programming language (nothing works unless you have it), Rails is the framework which allows us to build the web based application, and GIT is the SCM (Source Code Management) system we will use to push our code to our server.

For server technology, the easiest is to use Heroku (Heroku.com) – a completely managed system. You can get started for free, with upgraded capacity, speed etc added at extra monthly cost. If you’re comfortable setting up your own server, you may wish to use the likes of DigitalOcean.

It must be noted that shared hosting does not work for Ruby based applications. You not only need GIT access (typically through SSH) but the server is also required to run Ruby as a running process. This cannot be done with shared hosting (unfortunately).

Installing Ruby & Rails

The first step to programming a RoR application is to install Ruby & Rails on your system.

Whilst there are different ways to do this, depending on which platform you’re running (Windows/Linux etc), there is a core set of steps to follow:

 

  1. Install Ruby
    This is done either from source, or by using a pre-compiled version. If you’re using Windows, you will need to install each component separately.
  2. Install RubyGems
    This is the base set of protocols which allows you to download all the extra libraries for Ruby – the “gems”. These gems are used to provide swathes of functionality for Ruby web development. Part of what made Ruby extremely attractive in the first place was this extensive set of extensible functionality. Rails is a gem, for example.
  3. Install Build Tools
    In Unix systems, you’ll want to install the “build essential” library, Windows will require installing the MSYS2 toolset. Both of these provide the system with the necessary tools to compile the myriad of self-building gems (such as MYSQL2 and RMagick).
  4. Install Rails
    After this, you can run “gem install rails” to get rails installed. This will place all Rails’ binaries onto your system, giving you the ability to develop with the framework.
  5. Install an IDE
    An IDE (Integrated Development Environment) is the software used to input code into the system. Whilst they are just glorified text editors, they do give you such functionality as linting, code highlighting etc. We use Atom but you can also use Sublime Text or a swathe of thers. If you feel confident, you may just want to use Notepad.
  6. Install GIT
    GIT is an SCM (source code management) system. It gives you the ability to create a “repository” and push it to an external web server. This technology / technique is basically like FTP on steroids, and is the primary way that Ruby code is “pushed” to servers. You have to download GIT separately on your system (from git-scm.com)
  7. Start Coding
    With the above installed, you just need to start coding. To do this, you need to browse to a new folder, load up CMD and type “rails new [[app name]]”. After pressing “Enter”, the standard application files will be placed onto the hard drive, allowing you to edit them and test them on a local server. This is the start of your application.

 

Getting Started

Without getting into specifics, the key thing to remember with Rails applications is that they are “done for you”.

Rails has a convention called “convention over configuration”. This means that the Rails framework has been designed to give you as complete way as possible to build and deploy a web based application.

The framework is known as an “MVC” model framework (model, view, controller) – which means that each time you send a request to the application, it uses a combination of a “model”, “view” and “controller” to build a response.

As such, when you create the new Rails application on your system, you will quickly see a large number of folders. The only ones which matter (in the beginning) are those located in the /app directory.

In here, you’ll see the likes of “assets”, “views”, “models” and “controllers” folders. If this means nothing, don’t worry. I’ll explain the basis of how it all works here.

MVC has been around for many years.

It works in the same way for every implementation:

 

  • When an application receives a request, it routes the request to a controller
  • The controller then pulls data from the model (which talks to the database) and puts it into a view
  • The view is returned to the user

In the case of Rails, the “view” is an HTML file populated with the data from the model. For example, you may have the following basic setup for a simple “hello world” application: 

 

#config/routes.rb 

root “application#show”

#app/controllers/application_controller.rb

class ApplicationController < ActionController::Base

def show

@name = User.first

end

end

#app/views/application/show.HTML.erb

Hello <%= @name.first %>

#app/views/models/user.rb

class User < ActiveRecord::Base

# connects to the DB

# has schema of id | first | last | dob | created_at | updated_at

end

 

The above will allow you to send a request to “http://127.0.0.1/” and should receive the first name of the first database user.

Pushing To A Web Server

The final step to getting set up is to push to a live server.

Whilst you can use a VPS of your own (it has to be a VPS because shared hosting does not have SSH access, nor supports Ruby applications), the simplest and most effective way to get started is to just use Heroku.

We still use Heroku for staging server purposes (when you publish a web application, it’s recommend you use a “staging” server to test the application, and a “production” server to host the application publicly). I’ve made many mistakes before by cutting out the development server.

To push to a Heroku server (free), I’ll briefly explain the process:

 

  1. Open a Heroku account (I think you may need to provide card details – don’t worry, they don’t charge for their free tier. It’s to validate your identity so you don’t make illegal sites)
  2. Create an “app” in their dashboard
  3. Click onto the app – you’ll be given a “git” URL
  4. Copy this URL and head back to your Rails application
  5. In CMD, type “git add remote heroku [[heroku link]]” (replace [[heroku link]] with the git URL Heroku gave you)
  6. Press enter
  7. Now, package up your application (git add.) (git commit -am “First Push”)
  8. After inputting these lines, type “git push heroku master” and press Enter
  9. You’ll have to enter your credentials – do this and the repo should be sent to Heroku
  10. After this, Heroku will build the application and “deploy” it on its own namespace ([[app-name] herokuapp com)
  11. Browsing to this namespace will show you the app

After doing this, it’s up to you to then manage your deploy protocol in your own way etc. I would strongly advocate using Heroku for staging environment servers; you’ll likely want to use such services as DigitalOcean for production. 

Further Developments

Of course, web application development is moving forward constantly.

Due to the extremely low barriers to entry (free) and the breadth of resources available, many people are drawn to Ruby on Rails development.

However, don’t let it fool you. The scope for earning decent money from this profession is entirely dependent on two factors – your skillset/ability, and your access to a market.

Unfortunately for many would-be developers, their epic dreams of creating the next Groupon / Stripe etc are shattered when they enter the “real world” — where clients don’t care about the code you use and just want the cheapest solution that barely works.

The key for any Rails developer is to keep investing into their programming skillset, even with other languages. Ruby spoils many developers because of its simplicity. Moving higher up the programming value chain (into the realms of C / C++ etc) open up a lot more stable positions.

It is therefore my recommendation that you continually push yourself to look at new UI ideas, new ways of doing things and generally improving your skillset as required. Attend hackathons, meet other developers and generally improve your exposure to the computing industry. This should present opportunities for you as you develop.

AKDSEO

Next Post

Building Your Empire From the Ground Up - The Essential MBA

Tue Apr 5 , 2022
It may be the hardest thing in the world to make ends meet. And when you don’t even have a degree, it becomes a thousand times harder unless you have the looks to become a Hollywood actress, the non-conformist brains to become an unorthodox self made millionaire, or the right […]
Universitas Swasta di Bandung