[Ruby] Ruby on Rails 5 Quick Learning Practice Guide 5.2 compatible Chapter 2

1 minute read

Chaptrt2 Let’s take a look at the Rails application

Windows 10 environment construction There is already a virtual environment with HyperV Manager in the lab. Ubuntu 18.04.3 installed rbenv 1.1.2-30-gc879cb0 Install from GitHub using Git. Ruby 2.5.1 installation Rails installation Install Node.js

When using JavaScript, it has a function to compress JavaScript for efficient delivery, but it is installed because a JavaScript runtime is required for it.

Install and set up PostgreSQL Start PostgreSQL. sudo service postgresql start

sudo su postgres -c'createuser -s {Ubuntu login user}' When I hit, I got the following error.

psql: FATAL: role “postgres” does not exist I entered the Ubuntu login user name without {} and it passed.

Use scaffold to automatically generate a “user management application” with the name scaffold_app using Rails commands. rails new scaffold_app -d postgresql

The rails new command creates a template for a Rails application. Specify the application name as scaffold_app. The scaffold_app directory and related files are created and logged. After this, bundle install is automatically executed, and the gem required to run Rails is installed. Creating a database. Based on the definition of the file config/databese.yml.

bin/rails s db:create

A script called rails in the bin directory directly under the application root directory is called by “bin/rails” instead of “rails”. Rails command can be executed in an environment where gem as Gemfile can be used. Rails server startup bin/rails s

rails uses Puma as a standard HTTP server.

Confirm server startup with WEB browser Enter http://localhost:3000/ in the address bar.

Create a template for user management function Automatically generate scaffolds about “users”. bin/rails generate scaffold user name:string address:string age:integer

**Create a table in the database for user management. ** bin/rails db:migrate

Display user list screen http://localhost:3000/users Access by entering the URL above on a WEB browser.

Points I thought were important

  • The function “CRUD” required for managing users has been generated. ** “Create”, “Read”, “Update”, “Delete” **
  • Code passage Rails application is configured with the concept of MVC (Model, View, Controller). The controller is called. In the controller, a method called “action” that describes the processing for the request is defined (idnex action when accessing the list screen).