loftwah / rails-blog

My Rails 7 Blog

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

My Rails Tutorial

Getting Started with Rails | API | Awesome Ruby | Awesome Rails | More than "Hello World" in Docker: Build Rails + Sidekiq web apps in Docker | Deploy Rails in Amazon ECS

Rails 7 Blog

This project is to teach myself Ruby on Rails. I am using Ubuntu in WSL2 (Windows Subsystem for Linux 2) and Ruby 2.7. This project requires Ruby and SQLite3. I will be using this guide to get started.

Table of Contents

Installation

Install gpg2

sudo apt install gnupg2 -y

Import keys and install RVM (Ruby Version Manager)

curl -sSL https://rvm.io/mpapis.asc | gpg2  --import -
curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
\curl -sSL https://get.rvm.io | bash -s stable
source /home/loftwah/.rvm/scripts/rvm
rvm install 2.7 # Rails 7 requires Ruby 2.7

Check that Ruby is installed.

ruby -v        
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]

Install SQLite3

sudo apt install sqlite3 -y
sqlite3 --version
3.31.1 2020-01-27 19:55:54 3bfa9cc97da10598521b342961df8f5f68c7388fa117345eeb516eaa837balt1

Jnstall Rails

gem install rails
rails --version
Rails 7.0.3.1

We can now create a new Blog application as per the guide we are following.

rails new blog

Note: rails new --help will show you all the options available.

The following directories will be created and this is the explanation of what they are and what they do.

File/Folder Purpose
app/ Contains the controllers, models, views, helpers, mailers, channels, jobs, and assets for your application. You'll focus on this folder for the remainder of this guide.
bin/ Contains the rails script that starts your app and can contain other scripts you use to set up, update, deploy, or run your application.
config/ Contains configuration for your application's routes, database, and more. This is covered in more detail in Configuring Rails Applications.
config.ru Rack configuration for Rack-based servers used to start the application. For more information about Rack, see the Rack website.
db/ Contains your current database schema, as well as the database migrations.
Dockerfile Contains the Dockerfile and container requirements for your application.
docker-compose.yml Contains instructions on how to run your application in a Docker container using Docker Compose.
Gemfile & Gemfile.lock These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see the Bundler website.
lib/ Extended modules for your application.
log/ Application log files.
public/ Contains static files and compiled assets. When your app is running, this directory will be exposed as-is.
Rakefile This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails. Rather than changing Rakefile, you should add your own tasks by adding files to the lib/tasks directory of your application.
README.md This is a brief instruction manual for your application. You should edit this file to tell others what your application does, how to set it up, and so on.
storage/ Active Storage files for Disk Service. This is covered in Active Storage Overview.
test/ Unit tests, fixtures, and other test apparatus. These are covered in Testing Rails Applications.
tmp/ Temporary files (like cache and pid files).
vendor/ A place for all third-party code. In a typical Rails application this includes vendored gems.
.gitattributes This file defines metadata for specific paths in a git repository. This metadata can be used by git and other tools to enhance their behavior. See the gitattributes documentation for more information.
.gitignore This file tells git which files (or patterns) it should ignore. See GitHub - Ignoring files for more information about ignoring files.
.ruby-version This file contains the default Ruby version.

Running the Application

To start the web server, run the following command.

bin/rails server

This starts the Puma web server on port 3000. Navigate to localhost:3000 to see the application.

Docker

Ruby | PostgreSQL | Adminer

We can run this application in a Docker container using Docker Compose. If you don't have Docker installed please click here for the instructions.

docker-compose build
docker-compose up # or if you want it in the background
docker-compose up -d

Screenshots

Screenshot Description
Rails 7 Blog Blog Articles
Rails 7 Blog Read Article
Rails 7 Blog Adminer

Ruby examples

These are some examples of how to do things in Ruby.

  • basics
"Dean" #=> String
7         #=> Integer
6.9       #=> Float
[1,2,3]   #=> Array
(1..10)   #=> Range
false     #=> FalseClass (Boolean)
true      #=> TrueClass (Boolean)
nil       #=> NilClass
name = "Dean"
name.upcase
# Print the result of 4 + 8 to the terminal
puts 4 + 8
puts name # "Dean" "DEAN"
  • methods
# DRY (Don't Repeat Yourself)!!!!!
# puts "Hello Dean, how are you?"
# puts "Hello Scott, how are you?"
# puts "Hello Stephanie, how are you?"
# puts "Hello Luke, how are you?"
# method definition (the code inside WON'T RUN until the method is called!)
# def greet_person
#   puts "HI!"
# end
# DRY method with one parameter (name)
def greet_student(name)
  puts "Hello #{name}, how are you today? :)"
end
# we ommit the () characters if we don't have arguments!
# greet_person
# greet_person
# we're calling the method greet_student with the ARGUMENT 'Dean'
greet_student('Dean')
greet_student('Scott')
greet_student('Stephanie')
greet_student('Luke')
def full_name(first_name, last_name)
  first_name = first_name.capitalize
  last_name = last_name.capitalize
  return "#{first_name} #{last_name}"
end
# full_name is returning nil!
name = full_name('deAn', 'loFtS')
puts name
def max(number_1, number_2)
  return 0 if number_1 == number_2
  if number_1 > number_2
    return number_1
  else
    return number_2
  end
end
maximum = max(5, 6) # => 6
puts maximum # => 6
def upcased_name(name)
  5 + 6
  new_name = name.downcase
  # return new_name
  name.upcase
end
upcased = upcased_name('dean')
puts upcased
def stupid_method
end
result = puts('Dean')
puts result
require 'date'
def tomorrow
  tomorrow_date = Date.today + 1
  return tomorrow_date.strftime("%B %d")
end
puts tomorrow   # => "September 2"
  • strings
puts "Gday mate"
puts 'Gday mate'
name = 'Dean'
age = 36
# Concatenation of strings
# puts 'Hello' + ' ' + name + ' ' + 'how are you?'
puts 'You are' + ' ' + 36.to_s + ' ' + 'years old'
# Interpolation of strings (needs double quotes!)
# puts "Hello #{name} how are you?"
puts "You are #{35 + 1} years old"
  • variables
# variable names should be lower snake case
user_city = 'Melbourne'
age = 36 # assigning a variable value
age_in_four_years = age + 4
age = age + 4 # re-assignment
age += 4 # sugar syntax to age = age + 4
puts age #=> 40
age = 40 # re-assignment
puts age #=> 40

Todo

  • Add postgresql

About

My Rails 7 Blog

License:MIT License


Languages

Language:Ruby 73.2%Language:HTML 19.1%Language:JavaScript 3.5%Language:CSS 2.0%Language:Dockerfile 1.5%Language:Shell 0.7%