Ruby on Rails

Ruby on Rails is a server-side web application framework written in the Ruby programming language. Rails is a model-view-controller (MVC) framework, providing default structures for a database, a web service, and web pages.

class UsersController < ApplicationController
  def new
    @user = User.find(params[:id])
  end
end

Ruby on Rails Basics

Creating & Running a New Rails Application

rails new my_rails_app

rails _7.1.3.4_ new my_rails_app
cd my_rails_app

rails server

Parts of a Rails Application

Routes

This file will be saved as config/routes.rb.

Rails.application.routes.draw do
  # Root path ('/')

  root 'welcome#index'

  # Different ways to create routes that go to different controller actions

  get '/about', to: 'static#about'
  post '/about', to: 'static#about'

  resources :dashboard, only: [:index]
  resources :drinks, only: [:index, :new, :create, :edit, :update, :destroy]
  resources :support, only: [:index]

  # Extra

  get '*path' => redirect('/')
  get 'up' => 'rails/health#show', as: :rails_health_check
end

Controllers

This file will be saved as app/controllers/welcome_controller.rb.

class WelcomeController < ApplicationController
  def index
    # Find all records
  end

  def show
    # Find a specific record
  end

  def new
    # Empty form to create a new record
  end

  def create
    # Create a new record on the database
  end

  def edit
    # Filled in form to update an existing record
  end

  def update
    # Update an existing record on the database
  end

  def destroy
    # Delete an existing record on the database
  end

  private

  def record_params # Strong params are required to create or update a record
    params
      .permit(:one, :two)
      .require(:three, :four)
  end
end

Models

This file will be saved as app/models/record.rb.

class Record < ApplicationRecord
  # Validations

  # Relationships

  # Enums

  # Queries or Methods
end

Views

This file will be saved as app/views/welcome/index.html.erb.

<h1> Welcome to the index page! </h1>

<p> This is the page associated with the index action in the Welcome controller. </p>

<p> <%= 'These pages use embedded ruby (erb) to dynamically display content.' %> </p>