Why Ruby on Rails Is Still Worth Your While as a Developer
You can do a lot worse than start your full-stack life with Ruby on Rails — not everything needs to be JavaScript-driven.
Jul 6th, 2024 7:00am by
Image via Unsplash.
Continuing Our Rails Config
In our last post, in which I showed you how to do a “full fat install” of Ruby on Rails, we were just getting into some object relational mapping (ORM). First we installed Ruby, Rails and sqlite3. We wrote a route, with a controller, and started the server. Then we created a model and our first migration (below), which is the description of a record. We are following on from this Rails guide.
class CreateArticles < ActiveRecord::Migration[7.1]
def change
create_table :articles do |t|
t.string :title
t.text :body
t.timestamps
end
end
end
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: storage/development.sqlite3
Cool, but what exactly did that do? Well, wisely the guide suggests that we plunge into the Rails console. This is like the live Ruby console, irb, but for Rails, and it groks the project’s Ruby source. Note how the completion already knows about our Article model, and the ArticlesController:
I know I can ask the model for all the existing Article objects, even though there should not be any as yet:
This clearly references the appropriate SQL, and believes the schema for the articles table is available. In which case, we can create an article and it should pop it in the table:
We can see how the object, once saved, is inserted via SQL into the table. The console is a good way to demystify ActiveRecord. But we are here to make a website, so let’s get back to the project source.
When we hit our one route, we want to fetch all the articles and then let the view show them all. This is the typical Model-View-Controller (MVC) flow; the data is in the model, the controller intercepts the REST request defined in the route, then the view shows the required data.
So the controller (i.e. app/controllers/articles_controller.rb) needs to load the articles:
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
end
@articles represents a local variable, but one that the view can also read.
Now for the view; we will use the templating language ERB. We looked at examples of this in the Hotwire article, and it is pretty simple to pick up. We replace app/views/articles/index.html.erb with:
<h1>Articles</h1>
<ul>
<% @articles.each do |article| %>
<li>
<%= article.title %>
</li>
<% end %>
</ul>
You can feel the flow here; route to controller to view. Because we know we will eventually exploit all of the CRUD elements through REST, we can jump ahead a bit here and prepare all the resources at once. Rails provides a specially named resources route that maps all of the conventional routes at once.
If we asked Rails to list all the routes defined for our articles, we get the following:
If we now define the resources route for articles within routes.rb:
Rails.application.routes.draw do
get "/articles", to: "articles#index"
resources :articles
end
From here, you can just implement the corresponding controller methods one by one.
I think we have now gone far enough to understand the guts of the Rails system. You might be surprised at how much you can do as a developer with Ruby on Rails. Not everything has to be JavaScript-focused these days, so why not go back to the future with Rails?
See also: Return to the Rails Way: Installing Ruby on Rails in 2024
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.