Ruby_On_Rails

Ruby on Rails, often referred to as Rails, is a powerful and full-featured web application framework built using the Ruby programming language. Rails follows the convention-over-configuration principle, allowing developers to focus on writing application logic rather than spending time on tedious configuration. It emphasizes the use of best practices and offers a set of sensible defaults for database modeling, routing, and view rendering. Rails includes a robust ORM (Object-Relational Mapping) called ActiveRecord, which simplifies database interactions and promotes code reuse.

Module 1: Introduction to Ruby on Rails

What is Ruby on Rails?

Ruby on Rails, or simply Rails, is an open-source web application framework written in Ruby. It is a model-view-controller (MVC) framework, providing default structures for a database, a web service, and web pages.

Setting Up a Rails Project

A new Rails project can be created with a single command in the terminal.

rails new myapp

Module 2: Rails Fundamentals - MVC, Routing, and Controllers

MVC Architecture

Rails follows the MVC architecture where Models represent database data, Views display the data, and Controllers handle user interaction and business logic.

// Rails doesn't have a direct code example for this section.

Routing

Routes in Rails determine how an HTTP request is routed to your controller's actions. They are defined in the `config/routes.rb` file.

get 'welcome/index', to: 'welcome#index'

Controllers

Controllers respond to user requests, and provide it to the Model and View. Controllers are the middleman between Models and Views.

class ApplicationController < ActionController::Base
end

Module 3: Working with Databases in Rails

Active Record Basics

Active Record is the M in MVC, the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of objects whose data requires persistent storage to a database.

class Product < ApplicationRecord
end

Migrations

Migrations are a convenient way to alter your database schema over time in a consistent and organized manner.

class CreateProducts < ActiveRecord::Migration[5.0]
  def change
    create_table :products do |t|
      t.string :name
      t.text :description

      t.timestamps
    end
  end
end

Module 4: Views and Layouts in Rails

Working with Views

Views are what the user sees, they represent the V in the MVC pattern. In Rails, views are often HTML files with embedded Ruby code.

<%= @article.title %>
<%= @article.text %>

Layouts and Rendering

With layouts and rendering, you can decide how to combine the current view with different templates and partials, providing great flexibility in building up your final webpage.

<%= render 'shared/menu' %>
<%= yield %>

Module 5: Rails Security

Authentication

Rails provides several tools to help you implement user login/logout functionality and protect your pages from unauthenticated access.

class SessionsController < ApplicationController
  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      log_in user
      redirect_to user
    else
      render 'new'
    end
  end
end

Authorization

Authorization in Rails can be handled using a variety of techniques, and it ensures that users can only perform actions they are permitted to.

class UsersController < ApplicationController
  def edit
    @user = User.find(params[:id])
    redirect_to(root_url) unless current_user?(@user)
  end
end

CSRF Protection

Rails comes with built-in CSRF protection in the form of a token placed in a meta tag. This token is required any time a non-GET request is made.

<%= csrf_meta_tags %>