[RUBY] About Rails 6

Premise

I will write what I learned about Rails 6.

macOS Docker / Docker Compose installed

Main subject

Reasons to use a virtual environment

I'm having trouble developing a Rails application. That is, the environment of the development machine (working machine) and the machine for actual operation (production machine) are different. The environment mentioned here refers to the type and version of the OS, or the type and version of software and libraries running on the OS. Ruby on Rails is designed to work on Windows, MacOS, and Linux. However, this is not always the case with Gem packages that are introduced to extend Rails. Even if it works, the behavior may be slightly different or problems may occur in a specific environment.

The solution to this problem is to match the environment of the work machine and the production machine. There is an option called ** Virtual Environment **. Build a virtual environment on a desktop OS such as Windows, MacOS, ubuntu, and install a Linux-based Server OS there.

Rails applications under development run in a virtual environment. Access the Rails application from a web browser on the desktop OS and check the operation. On the other hand, the Rails application source code is opened and edited on the desktop OS by the function of the shared folder. This way, you can continue to use your familiar text editor and IDE while developing Rails applications in the same environment as your production machine.

What is Docker

Open source software that provides a virtual environment. The ease of setting and quick start-up are popular. Each virtual environment is called a container. The contents of the container are described in a text file called Dockerfile. With this file you can restore the container on various operating systems.

What is Docker-compose?

When developing a Rails application or running it in a production environment using Docker, it is common to build the Rails application and database server as separate containers. Docker-compose is a tool for starting and stopping these multiple containers at once. Just execute the command docker-compose up </ code> and all the containers that make up the Web application will start working.

Build Rails development environment

Build a Rails development container group using Docker and Docker Compose.

terminal.


$ git clone https://github.com/oiax/rails6-compose.git
$ cd rails6-compose
$ ./setup.sh

Start and stop containers

To start all the containers, execute the following command in the terminal. Option -d is for running the container as a daemon (background process).

terminal.


$ docker-compose up -d

To stop the containers, execute the following command in the terminal.

terminal.


$ docker-compose stop

Log in to the web container

terminal.


$ docker-compose exec web bash

Rails version check

bash-4.4$ rails --version

If the result is output as Rails 6.0.0, it's OK!

Create a new Rails application with the rails new command.

bash-4.4$rails new app name-d postgresql --skip-test-unit

--skip-test-unit </ code> is an option to skip the generation of Test :: Unit related code.

Check the contents

bash-4.4$ ls -a app name/

Editing Gemfile

gem "bcrypt"         #Password encryption
gem "rails-i18n"     #A collection of translation files such as error messages, dates, times, currency units, etc. output by rails
gem "kaminari"       #Pagination function
gem "date_validator" #Validate the date
gem "valid_email2"   #Validate to email address
gem "nokogiri"       #XML/For HTML analysis / generation
  • Email_validator has become less practical due to looser validation standards due to version upgrades. For example, an e-mail address containing two @s is judged to be valid.

Recommended Posts