DEV Community

Cover image for Exploring Web Development: Ruby on Rails
Alex Hebert
Alex Hebert

Posted on • Updated on

Exploring Web Development: Ruby on Rails

What is Ruby?

Ruby is a general purpose programming language that features simple and beginner-friendly syntax while being a powerful tool for experienced developers. Ruby is an interpreted language and is dynamically typed similarly to JavaScript and Python making my transition to Ruby fairly simple.

There are a few things I found while learning ruby that I found interesting. For example, ruby has an unless statement along with the if statement that is a negated version of an if. Ruby is a generally more readable language than more symbol filled languages like JavaScript.

What is Rails?

Rails is a full-stack web framework for Ruby that is "Optimized for happiness." It uses the MVC framework pattern which they call "Active Records", "Action Views", and "Action Controllers". Ruby on Rails was made to take the tedium and repetition out of web development. The main way it does this is 'Convention over Configuration.'

Rails is a very opinionated and strict framework in order to make all its magic work. Things like naming conventions are very important to the Rails framework to make everything work correctly. To this end, the amount of customization you get with Rails is fairly limited compared to something like created a MERN stack app. But because the configuration is baked in its very easy to jump straight into the hacking with Rails.

The lack of configuration does not hold Rails back either, many very large and popular sites are built with rails like GitHub, AirBnB, Shopify and Square. Before jumping into project set up, lets take a look at the 3 main components of the Rails MVC framework.

Project Setup

Lets make a to-do app with Rails! We will walk through project setup, defining a route, generating a controller and writing a view.

Install Rails

Assuming you have Ruby and your preferred database installed, we need to install Rails. Running this command will download the gem:

$ gem install rails 
Enter fullscreen mode Exit fullscreen mode

Creating the App

Rails makes it very simple to get your development environment set up. The rails cli command will create your directory for you:

$ rails new <app-name>
Enter fullscreen mode Exit fullscreen mode

And that's it, the skeleton for the app is set up. The majority of the work is done in the app directory. Here you'll find the models, views, controllers and helpers for your app. The config directory houses all the configuration for the app including for the database and routes.

Running the Server

Since rails took care of all the set up work for us, we can immediately run our server with:

$ bin/rails server
Enter fullscreen mode Exit fullscreen mode

This will spin up a server on localhost:3000 that watches for changes made and restarts automatically.

Making the App

Define a Route

The first thing to do to implement new functionality to a Rails app is to make a route to accept and map a request to a controller. The route configuration is in config/routes.rb. Here is where you will define all the endpoints for your app. For now lets add a route to get the to-dos. Routes are written in Ruby DSL(Domain-Specific Language) and take the shape of <HTTP Verb> <enpoint>, to: <controller>#<action>. to map GET /todos to the index action of the TodosController we add this to config/routes.rb:

Rails.application.routes.draw do
  get "/todos", to: "todos#index"
end
Enter fullscreen mode Exit fullscreen mode

Generate a Controller

With Rails, rather than writing controllers from scratch, they can be generated. When Rails generates a controller, it also generates associated unit tests and helpers for the controller. To generate a controller for the GET /todos route we just added, run the command:

$ bin/rails generate controller Todos index --skip-routes
Enter fullscreen mode Exit fullscreen mode

This command generates a TodoController with an index action. We use --skip-routes since we already defined the route we want to use.
The controller we just generated is housed in app/controllers/todos_controller.rb. Right now our controller should look like this:

class TodosController < ApplicationController
  def index
  end
end
Enter fullscreen mode Exit fullscreen mode

Notice the lack of code here. This is a great example of "Convention over Configuration." If an action does not render a view explicitly it will default to rendering the view that matches the controller.

Create a View

One of the things generated alongside our controller was the file app/views/todos/index.html.erb, the default view for the TodoController. Here we can use HTML and Ruby code to make a template for our data. For now lets just change the file to say hello by adding

<h1>Hello! Welcome to your Todo List!</h1>
Enter fullscreen mode Exit fullscreen mode

Now we can navigate to http://localhost:3000/todos to see our welcome message. With a handful of commands and even fewer lines of code we now have a route that accesses a controller with an action that renders a view!

Final Thoughts

Rails is a genuinely fun framework to work with. Removing the tedium of writing the same logic over and over for each feature makes development lightning fast. Not only is Rails simple but its also fully fleshed out and powerful, no wonder so many companies opt to use rails for their web apps. While there is a bit of a learning curve once you get into the nitty gritty of fleshing out an app with Rails, I think it is well worth the work. If youre interested in continuing to explore rails, I recommend heading over to the Rails guides and following their getting started tutorial. Good luck and have fun developing!

Top comments (0)