100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Ruby on Rails Basics Cheat Sheet

Ruby on Rails Basics Cheat Sheet

Covers the Rails CLI, MVC routing and controllers, ActiveRecord models, and core Rails naming conventions for building web apps.

2 PagesBeginnerMar 30, 2026

Rails CLI

Essential commands for scaffolding and running a Rails app.

bash
rails new myapp --database=postgresql   # Create new appcd myapprails generate model Post title:string body:text   # Generate model + migrationrails generate controller Posts index show          # Generate controllerrails db:create                                     # Create databaserails db:migrate                                    # Run migrationsrails server                                        # Start dev server (localhost:3000)rails console                                        # Interactive REPL

Routes & Controllers

Defining RESTful routes and a resourceful controller.

ruby
# config/routes.rbRails.application.routes.draw do  resources :posts            # generates index/show/new/create/edit/update/destroy  root "posts#index"end# app/controllers/posts_controller.rbclass PostsController < ApplicationController  def index    @posts = Post.all  end  def show    @post = Post.find(params[:id])  end  def create    @post = Post.new(post_params)    if @post.save      redirect_to @post, notice: "Post created."    else      render :new, status: :unprocessable_entity    end  end  private  def post_params    params.require(:post).permit(:title, :body)  endend

Models & ActiveRecord

Associations, validations, and common query methods.

ruby
# app/models/post.rbclass Post < ApplicationRecord  belongs_to :author, class_name: "User"  has_many :comments, dependent: :destroy  validates :title, presence: true, length: { maximum: 100 }end# ActiveRecord query examplesPost.where(published: true).order(created_at: :desc).limit(5)Post.find_by(title: "Hello World")post.comments.create(body: "Nice post!")

MVC Conventions

Naming and structure conventions Rails relies on.

  • Model naming- Singular, CamelCase class name (Post) mapped to a plural, snake_case table (posts)
  • Controller naming- Plural, CamelCase class name ending in Controller (PostsController) under app/controllers
  • Migrations- Timestamped files in db/migrate/, applied in order via rails db:migrate
  • Convention over configuration- Rails infers foreign keys (post_id), table names, and view paths automatically from class names
  • Views- app/views/<controller>/<action>.html.erb renders by default, matching the action name
  • Gemfile / Bundler- Gemfile plus bundle install manage gem dependencies; config/environments/ holds per-environment settings
Pro Tip

Always mass-assign through strong parameters (params.require(:model).permit(...)) in every controller action — skipping it opens the door to mass-assignment vulnerabilities.

Was this cheat sheet helpful?

Explore Topics

#RubyOnRailsBasics#RubyOnRailsBasicsCheatSheet#Programming#Beginner#RailsCLI#RoutesControllers#ModelsActiveRecord#MVCConventions#WebDevelopment#CommandLine#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet