Ruby on Rails is a full-stack web application framework following the Model-View-Controller (MVC) pattern and "Convention over Configuration".
Sub-Cheatsheets in this Guide
- Rails Models Cheatsheet
- Rails Controllers Cheatsheet
- Rails Routes Cheatsheet
- Rails Database Migrations Cheatsheet
- Rails Form Helpers Cheatsheet
- Rails View Helpers Cheatsheet
- Rails Internationalization (i18n) Cheatsheet
- Rails Tips & Tricks Cheatsheet
Common CLI Commands
# Generate a new Rails project
rails new myapp --database=postgresql
# Start development server on localhost:3000
bin/rails server
# Start interactive Rails console
bin/rails console
# Run database migrations
bin/rails db:migrate
Controller & Action Conventions
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
def index
@projects = Project.all
end
def show
end
private
def set_project
@project = Project.find(params[:id])
end
end
Layouts & Yielding
In app/views/layouts/application.html.erb:
<!doctype html>
<html>
<head>
<title><%= content_for?(:title) ? yield(:title) : "Default App" %></title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "application" %>
<%= javascript_importmap_tags %>
</head>
<body>
<%= yield %>
</body>
</html>