Skip to content
Ruby on Rails Reference & Cheatsheet

Ruby on Rails Reference & Cheatsheet

DodaTech Updated Jun 6, 2026 3 min read

Learning Path

    flowchart LR
    A["Ror Overview"] --> B["Core Concepts"]
    B --> C["Intermediate Topics"]
    C --> D["Advanced Topics"]
    D --> E["Practical Applications"]
    A --> F["You Are Here"]
    style F fill:#f90,color:#fff
  

A comprehensive reference for Ruby on Rails development — Active Record, migrations, routing, controllers, views, authentication with Devise, and production deployment.

Core Commands

rails new myapp -d postgresql           # New app with PostgreSQL
rails generate scaffold Product name:string price:decimal
rails generate migration AddStockToProducts stock:integer
rails db:create db:migrate               # Create DB + run migrations
rails server                              # Start dev server (localhost:3000)
rails console                             # Interactive Rails console

RESTful Routes

# config/routes.rb
Rails.application.routes.draw do
  resources :products do
    collection do
      get :search          # GET /products/search
    end
    member do
      post :publish        # POST /products/:id/publish
    end
    resources :reviews, only: [:create, :destroy]
  end

  namespace :admin do
    resources :users       # GET /admin/users, etc.
  end
end

Active Record

# Model with validations and associations
class Product < ApplicationRecord
  belongs_to :category
  has_many :reviews, dependent: :destroy

  validates :name, presence: true, length: { maximum: 100 }
  validates :price, numericality: { greater_than: 0 }
  validates :sku, uniqueness: true

  scope :available, -> { where("stock_count > 0") }
  scope :by_price, -> { order(price: :asc) }
end

# Query examples
Product.available
Product.where("price > ?", 10)
Product.order(created_at: :desc).limit(5)
Product.joins(:category).where(categories: { name: "Electronics" })

Controller

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]

  def index
    @products = Product.all
    respond_to do |format|
      format.html
      format.json { render json: @products }
    end
  end

  def create
    @product = Product.new(product_params)
    if @product.save
      redirect_to @product, notice: "Product created."
    else
      render :new, status: :unprocessable_entity
    end
  end

  private

  def set_product
    @product = Product.find(params[:id])
  end

  def product_params
    params.require(:product).permit(:name, :price, :description)
  end
end

Views (ERB)

<%# app/views/products/index.html.erb %>
<h1>Products</h1>
<%= link_to "New Product", new_product_path, class: "btn" %>

<table>
  <thead>
    <tr><th>Name</th><th>Price</th><th>Actions</th></tr>
  </thead>
  <tbody>
    <% @products.each do |product| %>
      <tr>
        <td><%= product.name %></td>
        <td><%= number_to_currency(product.price) %></td>
        <td>
          <%= link_to "Show", product %>
          <%= link_to "Edit", edit_product_path(product) %>
          <%= button_to "Delete", product, method: :delete %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

Authentication with Devise

# Gemfile
gem "devise"

# Terminal
rails generate devise:install
rails generate devise User
rails db:migrate

# Controller
before_action :authenticate_user!

# View helpers
user_signed_in?
current_user
user_session

Migration Types

add_column :table, :column, :type
remove_column :table, :column
rename_column :table, :old, :new
add_index :table, :column
add_reference :table, :other, foreign_key: true
create_join_table :table1, :table2

Common Mistakes

  1. Assuming all features work identically — always check browser/version compatibility.
  2. Skipping documentation — reference docs exist for a reason; consult them.
  3. Not testing edge cases — your setup may differ from tutorials.
  4. Overlooking security — always validate inputs and follow best practices.
  5. Copy-pasting without understanding — type code yourself to build real knowledge.

What’s Next

LessonDescription
LaravelLaravel PHP framework
ExpressExpress.js framework
RubyRuby programming language
REST APIRESTful API design
SQLSQL fundamentals

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro