Ruby on Rails: the Basic Idea – Ruby
MVC, convention over configuration, ActiveRecord and the typical flow of a Rails app.
Rails is a full-stack web framework following the MVC pattern (model, view, controller). Its guiding principle is convention over configuration: if you stick to the naming conventions, you have to configure almost nothing. The model Article belongs to the table articles, the controller ArticlesController to the views in app/views/articles/.
gem install rails
rails new blog
cd blog
bin/rails generate model Article title:string body:text
bin/rails db:migrate
bin/rails generate controller Articles index show
bin/rails server # http://localhost:3000The router maps URLs to controller actions. resources creates all the usual REST routes at once:
# config/routes.rb
Rails.application.routes.draw do
root "articles#index"
resources :articles, only: %i[index show new create]
endThe model (with ActiveRecord) maps a table as a class, including validations and associations. Migrations describe schema changes as Ruby code:
# app/models/article.rb
class Article < ApplicationRecord
has_many :comments, dependent: :destroy
validates :title, presence: true, length: { minimum: 3 }
scope :recent, -> { order(created_at: :desc) }
end
# db/migrate/20260101000000_create_articles.rb
class CreateArticles < ActiveRecord::Migration[8.0]
def change
create_table :articles do |t|
t.string :title, null: false
t.text :body
t.timestamps
end
end
endThe controller handles the request, loads data and chooses the view. Strong parameters define which form fields are allowed at all:
# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def index
@articles = Article.recent.limit(10)
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article, notice: "Saved!"
else
render :new, status: :unprocessable_entity
end
end
private
def article_params
params.require(:article).permit(:title, :body)
end
end<%# app/views/articles/index.html.erb %>
<h1>Artikel</h1>
<% @articles.each do |article| %>
<h2><%= link_to article.title, article %></h2>
<p><%= truncate(article.body, length: 100) %></p>
<% end %>Article.where(title: "Ruby").order(:created_at).limit(5)
Article.find(1)
Article.create!(title: "Hello", body: "First post")
Article.includes(:comments).each { |a| puts a.comments.size } # avoids N+1- ERB templates escape output by default (protection against XSS)
- CSRF protection is already built in (
protect_from_forgery, token in forms) - ActiveRecord uses prepared statements:
where(title: x)is protected against SQL injection - Generators, migrations, tests, mailers, jobs are part of the package
includes/preload. And never write SQL with string interpolation: where("title = #{params[:t]}") is an SQL injection – use where("title = ?", params[:t]).Article → articles), which often does not work for German words (Artikel → Artikels). Keep controllers slim ("fat model, skinny controller") and move logic into models or service classes.