Rails Tutorial 4th Edition: Chapter 1 From Zero to Deployment

Rails Tutorial 4th Edition: Chapter 1: From Zero to Deployment

hello_app

1.1 Introduction

1.1.1 Prerequisite knowledge

--Practice ―― 1. Which website has the Ruby gem used for Ruby on Rails?

―― 2. What is the latest version of Rails at the moment? - v6.0.3.4? https://guides.rubyonrails.org/

—— 3. How many times has Ruby on Rails been downloaded so far? Please check it out. --247,586,141 Maybe you can see it here. I was able to confirm the latest version. https://rubygems.org/gems/rails?locale=ja      

1.2 Move immediately

abridgement

1.3 First application

The Rails tutorial said:

Terminal


$ cd                  #Go to your home directory
$ mkdir environment     # 'environment'Create a directory
$ cd environment/       # 'environment'Go to the directory
$ cd ~/environment
$ rails _5.1.6_ new hello_app

Since the directory called environment was already created, I created it with another name and did not specify the version when rails new.

image.png

The Rails tutorial seemed to use something in the cloud, but I will do coding etc. with VS Code. I might switch to RubyMine on the way. Launch VS Code, open the file, and open hello_app.

image.png

If you open the VS Code terminal (TERMINAL), you should be in the root directory of your application.

image.png

1.3.1 Bundler

Terminal


-> % bundle install
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Using rake 13.0.1
Using concurrent-ruby 1.1.7
Using i18n 1.8.5
・
・
・
Using webpacker 4.3.0
Bundle complete! 17 Gemfile dependencies, 74 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

1.3.2 rails server

Terminal


-> % rails s  
=> Booting Puma
=> Rails 6.0.3.4 application starting in development 
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.6 (ruby 2.7.1-p83), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://127.0.0.1:3000
* Listening on tcp://[::1]:3000
Use Ctrl-C to stop

If you open http: // localhost: 3000 /, it will look like the figure below.

image.png

--Practice ―― 1. What version of Ruby do you have on your computer now compared to what you see on the default Rails page? You can easily see it by running ruby -v on the command line. I will. - 2.7.1

―― 2. In the same way, let's check the Rails version. Does the version you looked up match the version you installed in Listing 1.1? - 6.0.3.4
image.png

1.3.3 Model-View-Controller (MVC)

1.3.4 Hello, world!

app/controllers/application_controller.rb


class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  def hello
    render html: "hello, world!"
  end
end

config/routes.rb


Rails.application.routes.draw do
  root 'application#hello'
end
image.png

--Practice ―― 1. Let's rewrite the hello action so that "hola, mundo!" Is displayed instead of "hello, world!".

―― 2. Rails also supports "non-ASCII characters". “¡Hola, mundo!” Contains the Spanish-specific upside-down exclamation mark “¡” (Fig. 1.17) 19. To display the "¡" character on your Mac, hold down the Option key and press the 1 key. It may be faster to copy this character and paste it into your editor.

--3. Refer to the hello action in Listing 1.7 and add a second action, goodbye. This action displays the text "goodbye, world!". Edit the routing in Listing 1.9 to change the route routing assignment from the hello action to the goodbye action.

app/controllers/application_controller.rb


class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  def hello
    render html: "hola, mundo!"
  end

  def goodbye
    render html: "goodbye, world!"
  end
end

config/routes.rb


Rails.application.routes.draw do
  root 'application#goodbye'
end
image.png

1.4 Version control with Git

Reference

1.4.1 Installation and setup

abridgement

1.4.2 Benefits of Git

abridgement

1.4.3 Bitbucket This time I used Github.

Terminal


-> % git init
Reinitialized existing Git repository in /Users/fukadashigeru/environment_2/hello_app/.git/

-> % git add -A

-> % git commit -m "Initialize repository"
[master (root-commit) 02fb3dd] Initialize repository
 92 files changed, 9246 insertions(+)
 create mode 100644 .browserslistrc
 create mode 100644 .gitignore
 create mode 100644 .ruby-version
・
・
・
 create mode 100644 yarn.lock

-> % git remote add origin https://github.com/**********/hello_app.git

-> % git push origin master
Enumerating objects: 107, done.
Counting objects: 100% (107/107), done.
Delta compression using up to 4 threads
Compressing objects: 100% (89/89), done.
Writing objects: 100% (107/107), 149.16 KiB | 3.47 MiB/s, done.
Total 107 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), done.
To https://github.com/**********/hello_app.git
 * [new branch]      master -> master

1.4.4 Branch, edit, commit, merge

Terminal


[~/environment_2/hello_app] [master]
-> % git checkout -b modify-README
Switched to a new branch 'modify-README'

*********************************************
README.Modify md file
*********************************************

[~/environment_2/hello_app] [modify-README]
-> % git commit -a -m "Improve the README file"
[modify-README ba451c3] Improve the README file
 1 file changed, 4 insertions(+), 22 deletions(-)

[~/environment_2/hello_app] [modify-README]
-> % git checkout master
Switched to branch 'master'

[~/environment_2/hello_app] [master]
-> % git merge modify-README
Updating 02fb3dd..ba451c3
Fast-forward
 README.md | 26 ++++----------------------
 1 file changed, 4 insertions(+), 22 deletions(-)

[~/environment_2/hello_app] [master]
-> % git branch -d modify-README
Deleted branch modify-README (was ba451c3).

[~/environment_2/hello_app] [master]
-> % git push origin master

1.5 Deploy

1.5.1 Heroku setup

Gemfile


source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.1'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3', '>= 6.0.3.4'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

group :development, :test do
  # development,test,Move what was in the whole settings part of production here in development and test
  # Use sqlite3 as the database for Active Record
  gem 'sqlite3', '~> 1.4'
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

#Add pg to producition
group :production do
  gem 'pg'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Terminal


-> % heroku login --interactive
 ›   Warning: heroku update available from 7.35.1 to 7.46.0.
heroku: Enter your login credentials
Email [***********@gmail.com]: 
Password: ***********
Logged in as ***********@gmail.com

-> % heroku keys:add
 ›   Warning: heroku update available from 7.35.1 to 7.46.0.
Found an SSH public key at /Users/***********/.ssh/id_rsa.pub
? Would you like to upload it to Heroku? Yes
Uploading /Users/***********/.ssh/id_rsa.pub SSH key... done

-> % heroku create
 ›   Warning: heroku update available from 7.35.1 to 7.46.0.
Creating app... done, ⬢ thawing-brook-84469
https://thawing-brook-84469.herokuapp.com/ | https://git.heroku.com/thawing-brook-84469.git

1.5.2 Deploy to Heroku (1)

Terminal


-> % git push heroku master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 332 bytes | 332.00 KiB/s, done.
・
・
・
remote: Verifying deploy... done.
To https://git.heroku.com/thawing-brook-84469.git
   48ef406..7fc2c4e  master -> master

1.5.3 Deploy to Heroku (2)

https://thawing-brook-84469.herokuapp.com/

image.png

That is not a hello world. Maybe there was a procedure mistake somewhere. Well, no.

--Practice

abridgement

1.5.4 Heroku command

Terminal


-> % bin/rails c
Running via Spring preloader in process 5709
Loading development environment (Rails 6.0.3.4)
irb(main):001:0> ('a'..'z').to_a.shuffle[0..7].join
=> "cntwqkdr"
irb(main):002:0> exit

-> % heroku rename cntwqkdr            
Renaming ravudstc to cntwqkdr... done
https://cntwqkdr.herokuapp.com/ | https://git.heroku.com/cntwqkdr.git
Git remote heroku updated
 ▸    Don't forget to update git remotes for all other local checkouts of the app.

-> % heroku open
image.png

--Practice ―― 1. Execute the heroku help command to display the list of Heroku commands. Which command is the command to view the logs for the Heroku app?

Terminal


-> % heroku help
CLI to interact with Heroku

VERSION
  heroku/7.46.0 darwin-x64 node-v12.16.2

USAGE
  $ heroku [COMMAND]

COMMANDS
  access          manage user access to apps
  addons          tools and services for developing, extending, and operating your app
  apps            manage apps on Heroku
  auth            check 2fa status
  authorizations  OAuth authorizations
  autocomplete    display autocomplete installation instructions
  buildpacks      scripts used to compile apps
  certs           a topic for the ssl plugin
  ci              run an application test suite on Heroku
  clients         OAuth clients on the platform
  config          environment variables of apps
  container       Use containers to build and deploy Heroku apps
  domains         custom domains for apps
  drains          forward logs to syslog or HTTPS
  features        add/remove app features
  git             manage local git repository for app
  help            display help for heroku
  keys            add/remove account ssh keys
  labs            add/remove experimental features
  local           run Heroku app locally
  logs            display recent log output
  maintenance     enable/disable access to app
  members         manage organization members
  notifications   display notifications
  orgs            manage organizations
  pg              manage postgresql databases
  pipelines       manage pipelines
  plugins         list installed plugins
  ps              Client tools for Heroku Exec
  psql            open a psql shell to the database
  redis           manage heroku redis instances
  regions         list available regions for deployment
  releases        display the releases for an app
  reviewapps      manage reviewapps in pipelines
  run             run a one-off process inside a Heroku dyno
  sessions        OAuth sessions
  spaces          manage heroku private spaces
  status          status of the Heroku platform
  teams           manage teams
  update          update the Heroku CLI
  webhooks        list webhooks on an app

--Practice ―― 2. Use the command found in the exercise above to check the recent log of the Heroku app. What was the most recent event? (Remembering the command to look at this log will help you find bugs in your production environment)

Terminal


> % heroku logs
2020-10-19T13:29:19.529499+00:00 app[web.1]: => Run `rails server --help` for more startup options
2020-10-19T13:29:20.730825+00:00 app[web.1]: Puma starting in single mode...
2020-10-19T13:29:20.730870+00:00 app[web.1]: * Version 4.3.6 (ruby 2.7.1-p83), codename: Mysterious Traveller
2020-10-19T13:29:20.730871+00:00 app[web.1]: * Min threads: 5, max threads: 5
2020-10-19T13:29:20.730871+00:00 app[web.1]: * Environment: production
2020-10-19T13:29:20.731100+00:00 app[web.1]: * Listening on tcp://0.0.0.0:41511
2020-10-19T13:29:20.731446+00:00 app[web.1]: Use Ctrl-C to stop
2020-10-19T13:29:21.035771+00:00 heroku[web.1]: State changed from starting to up
・
・
・

1.6 Finally

abridgement

Recommended Posts

Rails Tutorial 4th Edition: Chapter 1 From Zero to Deployment
Rails Tutorial Chapter 1 From Zero to Deployment [Try]
Chewing Rails Tutorial [Chapter 1 From Zero to Deployment] Second Half
Rails Tutorial (4th Edition) Memo Chapter 6
OpenTracing Tutorial First Half
Chewing Rails Tutorial [Chapter 1 From Zero to Deployment] Second Half
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 4
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial 6th Edition Learning Summary Chapter 3
Rails Tutorial 6th Edition Learning Summary Chapter 8
Rails Tutorial (4th Edition) Summary
rails tutorial Chapter 6
rails tutorial Chapter 1
rails tutorial Chapter 7
rails tutorial Chapter 5
rails tutorial Chapter 9
rails tutorial Chapter 8
Rails tutorial (6th edition) Follow/unfollow background operation
Rails Tutorial record and memorandum # 1 "From installation to hello_app deployment + error handling"
Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 10 Notes
Rails Tutorial Chapter 3 Notes
Rails Tutorial Chapter 3 Learning
Rails Tutorial Memorandum (Chapter 3, 3.1)
Rails Tutorial Chapter 4 Notes
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Rails Tutorial Chapter 8 Notes
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
Rails tutorial (6th edition) Background operation of profile editing
Rails tutorial (6th edition) Background operation of login function
[Rails Tutorial Chapter 4] Rails-flavored Ruby
Rails tutorial (6th edition) Background operation of password reset function
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
[Rails Tutorial Chapter 5] Create a layout
rails tutorial chapter 10 summary (for self-learning)
Chewing Rails Tutorial [Chapter 2 Toy Application]
[Rails tutorial] Cannot deploy to Heroku/Change from hello, world to hola, mundo
Rails Tutorial (6th Edition) -Explanation of background operation for each function-
From pulling docker-image of rails to launching
[Rails] How to convert from erb to haml
How to use MySQL in Rails tutorial
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
rails tutorial
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 6
[Rails] I tried to raise the Rails version from 5.0 to 5.2
How to link Rails6 Vue (from environment construction)
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 3
I want to play with Firestore from Rails
Chewing Rails Tutorial [Chapter 3 Creating Almost Static Pages]