Tuesday, June 13, 2023

HOW TO USE TRIX-EDITOR

Trix is an editor for writing messages, comments, articles, and lists—the simple documents most web apps are made of. It features a sophisticated document model, support for embedded attachments, and outputs terse and consistent HTML.


$  rails new trix_editor --css tailwind

$  cd trix_editor

$  rails action_text:install

$  rails g scaffold post title content:rich_text

$  rails s



Thursday, June 8, 2023

Fix Ubuntu on WSL that failed to boot after reinstalling

When you reinstall Ubuntu on WSL (Windows Subsystem for Linux), you might encounter an error message preventing the system from booting.

eg: Failed to attach disk 'C:\Users\ahaya\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx' to WSL2: The specified file could not be found.
Error code: Wsl/Service/CreateInstance/MountVhd/ERROR_FILE_NOT_FOUND
Failed to attach disk 'C:\Users\ahaya\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx' to WSL2: The specified file could not be found.
Error code: Wsl/Service/CreateInstance/MountVhd/ERROR_FILE_NOT_FOUND
Failed to attach disk 'C:\Users\ahaya\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx' to WSL2: The specified file could not be found.
Error code: Wsl/Service/CreateInstance/MountVhd/ERROR_FILE_NOT_FOUND
Press any key to continue...

1. Launch Windows PowerShell (No Need Administrator)

2. Run "wsl -l" in PowerShell to get the registered distribution

eg: PS C:\Users\Name> wsl -l
Windows Subsystem for Linux Distributions:
Ubuntu (Default)

3. Unregister Ubuntu.

Run wsl --unregister Ubuntu in PowerShell to unregister Ubuntu.

eg: PS C:\Users\User> wsl --unregister Ubuntu
Unregistering.
The operation completed successfully.

4. Launch Ubuntu and perform the initialization process.

The initialization process begins when you launch Ubuntu after it has been unregistered. After that, you can create an Ubuntu account as follows.

eg: Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username:

Source: https://www.en.techgaku.com/fix-ubuntu-boot-error-on-wsl-after-reinstall/

Wednesday, February 15, 2023

HOW TO USE DEVISE GEM

$ rails new devise_app -j esbuild -css bootstrap


$ cd devise_app


$ bundle add devise


$ rails g devise:install


$ rails g devise User


$ rails g controller pages home


$ rails db:migrate


[config/routes.rb]
root 'pages#home'


[app/views/pages/home.html.erb]

<% if current_user %>
    <p>Logged in as <%= current_user.email %></p>
    <%= link_to "Logout", destroy_user_session_path, method: :delete, data: { turbo_method: :delete } %>
<% else %>
    <%= link_to "Login", new_user_session_path %>
    <%= link_to "Sign up", new_user_registration_path %>
<% end %>

🐞Source: devise

Friday, December 16, 2022

VS Code Editor

 Download VS Code Editor: Here

VS Code Extensions

Recommended

1. esbenp.prettier-vscode

2. formulahendry.auto-close-tag

3. hex-ci.stylelint-plus

4. dbaeumer.vscode-eslint

5. naumovs.color-highlight

6. DigitalBrainstem.javascript-ejs-support

Optional

7. ritwickdey.LiveServer

8. erikphansen.vscode-toggle-column-selection

9. vscode-icons

10. Auto Rename Tag


VS Code Extensions for Rails

1. VS Code Icons

2. GitHub Copilot

3. TabNine AI Autocomplete

4. Ruby

5. VSCode Ruby (Syntax Highlighting)

6. ERB Formatter

7. Prettier



Wednesday, July 13, 2022

How to install Rspec

Add rspec-rails to both the :development and :test groups of your app’s Gemfile:

# Run against this stable release
group :development, :test do
  gem 'rspec-rails', '~> 6.0.0'
end

# Download and install
$ bundle install

# Generate boilerplate configuration files
# (check the comments in each generated file for more information)
$ rails generate rspec:install
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

$ rails generate respec:model user
# Running spec
$ bundle exec rspec spec/models (Run all spec files)
$ bundle exec rspec spec/models/user_spec.rb (Run a single spec file)

$ rails generate rspec:system users
# Running spec
$ bundle exec rspec spec/system/users_spec.rb

Source: rspec-rails

Tip: ※$ rails generate

Wednesday, June 1, 2022

How to fix Yarn packages are out of date in terminal

 =====================================

  Your Yarn packages are out of date!

  Please run 'yarn install --check-files' to update.

=====================================


1. Delete node_modules

$ rm -rf node_modules


2. Delete yarn.lock file

$ rm -rf yarn.lock


3. Install yarn

$ yarn install


4. Upgrade yarn

$ yarn upgrade


5. Run server

$ rails s

Tuesday, May 31, 2022

AWS Deployment

There are two popular editors that you can run in a terminal: vim and emacs but this time we will use vim.

example: 

MacBook-Pro:~$ vim ./poge OR $ vim poge

Basic principles

-When editing, press i to enter edit mode. 

-When moving the cursor or executing a vim command, press the esc key to enter normal mode.

-To save the file, enter normal mode, enter :wq and press enter. 

-If you want to open the file and close it without doing anything, enter normal mode, type :q! and press enter.

How to check permissions

The command, ls -l or ls -la can be used to check what permissions a file or a folder has.

===============================================

LET'S START

$ cd .ssh

$ chmod 400 achieve-key.pem (downloaded key pair)


SSH to EC2 using a key pair

$ cd .ssh

$ ssh -i "achieve-key.pem" ec2-user@13.113.254.221 (example)


AWS EC2 instance initial settings

1. Switch to administrator privileges

[ec2-user@ip-1-250 ~]$ sudo su -

[root@ip-1-250 ~]#


2.Update package management software

Implement at EC2

# yum update -y


3. Time zone correction

Implement at EC2

ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

# vi /etc/sysconfig/clock


You can edit the file by pressing i to enter insert mode.


ZONE="Asia/Tokyo"

UTC=false


Exit the insert mode by pressing the esc key, type :wq and hit Enter.


4. Change language information

Implement at EC2

# vi /etc/sysconfig/i18n


LANG=ja_JP.UTF-8


5. Settings to optimize for the environment of your country

Implement at EC2

# vi /etc/cloud/cloud.cfg


Change repo_upgrade: security to repo_upgrade: none

Also newly add locale: ja_JP.UTF-8


6. Create swap area

Implement at EC2

# dd if=/dev/zero of=/swapfile bs=1M count=1024

# mkswap /swapfile

# swapon /swapfile

# vi /etc/fstab


After opening the file, add the following

/swapfile swap swap defaults 0 0


7. Create new users

Implement at EC2

# useradd app -G wheel

# visudo


When the file opens, remove # at the head of #% wheel ALL = (ALL) NOPASSWORD: ALL


SSH connection to AWS

1. Copy public key

The public key is saved in id_rsa.pub file.


Let's log out of EC2

# exit

$ exit


Perform the following locally

$ cat ~/.ssh/id_rsa.pub


Copy the public key displayed.

What to copy starts with ssh-rsa and finishes with the end of the email address.


2. Set public key on EC2

Implement at EC2

$ sudo su app

$ cd ~

Create #ssh directory (omitted if it already exists)

$ mkdir .ssh

#Give yourself only the right to execute

$ chmod 700 .ssh

Move to #ssh directory

$ cd .ssh

Create #authorized_keys file

$ touch authorized_keys

#Give edit permission to only yourself for the file

$ chmod 600 authorized_keys

Open the #authorized_keys file

$ vi authorized_keys


If you can open the file, add the public key you just copied into this file.

After editing the file, leave the AWS environment with exit.


3. Connect SSH to EC2 using the copied public key

Perform the following locally

$ cd ~/.ssh

$ ssh -i "/Users/Akihiro/.ssh/id_rsa" app@1.234.567.89 (example)


If you can connect as the app user, what you used to log in as ec2-user can now log in as app as shown below.


[app@ip-172-31-50 ~]$


This completes SSH connection.


4. Checking time zone and time

Implement at EC2

[app@ip-172-31-50 ~]$ date


Next, restart crond to correct the time difference of crond.

Implement at EC2

$ sudo service crond restart

$ sudo reboot

If you log out of EC2 here, log in again.


5. Remove ec2-user

Implement at EC2

$ sudo su -

# userdel ec2-user


Various installations on EC2

1. Install git

Implement at EC2

$ sudo su -

# yum -y install git

# git --version


2. Install rbenv

Implement at EC2

# git clone https://github.com/rbenv/rbenv.git /usr/local/rbenv

(Create a new file called rbenv.sh file)

# vi /etc/profile.d/rbenv.sh


Describe the following in the opened file.

export RBENV_ROOT=/usr/local/rbenv

export PATH="$RBENV_ROOT/bin:$PATH"

eval "$(rbenv init -)"


# source /etc/profile.d/rbenv.sh

# rbenv --version


3. Installation of ruby-build

Implement at EC2

# git clone https://github.com/rbenv/ruby-build.git /usr/local/rbenv/plugins/ruby-build


4. Check the list of Ruby versions that can be installed with rbenv

Implement at EC2

# rbenv install -list


5. Install the tools and libraries for installing Ruby

Implement at EC2

# yum -y groupinstall "Development Tools"

# yum -y install gcc-c++ glibc-headers openssl-devel readline libyaml-devel zlib zlib-devel libffi-devel libxml2 libxslt libxml2-devel libxslt-devel mysql-devel readline-devel

# yum -y install ImageMagick ImageMagick-devel


6. Install Ruby

Implement at EC2 (Install the version of Ruby you are currently using locally.)

# rbenv install 3.0.1

(It takes a while)


7. Check if Ruby can be installed on EC2

Implement at EC2

# rbenv versions


8. Change the version of Ruby used in EC2

Use the command rbenv global to switch the Ruby version of the entire system.

Now, let's switch the ruby version.

Implement at EC2

# rbenv global 3.0.1

# ruby -v


Check the version of bundler by running the following command on both EC2 and your local application directory.


$ bundler -v


Implement at EC2

# gem install bundler -v 2.3.13 (example)

# exit


Implement at EC2

$ cd /usr/local/rbenv/

$ sudo chown app version

$ sudo chown app shims

$ ls -la 


Implement at EC2

$ source /etc/profile.d/rbenv.sh

$ rbenv global 3.0.1


If you can execute the above command without any problem, check the version of ruby.

$ ruby -v


9. Install node.js on EC2

Check the version of nvm and node.js locally

Implement at local

$ nvm --version

$ node -v


Specify the version of nvm confirmed locally in the "Version" part below and install it.

Implement at EC2

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash (example)

$ . ~/.nvm/nvm.sh

$ nvm install v14.17.3 (example)

$ node -e "console.log('Running Node.js ' + process.version)"

Running Node.js v14.17.3


10. Installing yarn on EC2

Implement at EC2

$ npm install --global yarn

$ yarn -v


Database Settings EC2

1. Install PostgreSQL

Implement at EC2

$ sudo su -

# amazon-linux-extras install postgresql11 -y

# yum install postgresql-server postgresql-devel -y

# psql --version


2. Initial PostgreSQL Settings

Implement at EC2

# postgresql-setup initdb


3. Starting PostgreSQL

Implement at EC2

# systemctl start postgresql.service


4. Edit Settings file

Implement at EC2

# vi /var/lib/pgsql/data/postgresql.conf

(remove the # at the beginning and edit the setting from 'localhost' to '*')

#------------------------------------------------- ----------------------------- 

 # CONNECTIONS AND AUTHENTICATION 

 #------------------------------------------------- ----------------------------- 

 #-Connection Settings- 

 listen_addresses ='*' # what IP address(es) to listen on; 

 # comma-separated list of addresses; 

 # defaults to'localhost'; use'*' for all 

 # (change requires restart) 

 #port = 5432 # (change requires restart)


Implement at EC2

# vi /var/lib/pgsql/data/pg_hba.conf

At the bottom of this file, add the following:

local all all trust

host all all 0.0.0.0/0 md5


Also comment out the lower part of the file as follows (add a # at the beginning).

# "local" is for Unix domain socket connections only

To comment→ # local all all peer

# IPv4 local connections:

To comment→ # host all all 127.0.0.1/32 ident

# IPv6 local connections:

To comment→ # host all all ::1/128 ident


When you have finished editing, please save the file with :wq .


Implement at EC2

# systemctl restart postgresql.service


Create user and database

Implement at EC2

# psql -U postgres

※​When connecting to PostgreSQL, the prompt will be postgres=#


[Important]

In PostgreSQL, we use the concept of roleto manage permissions on the database, but there are points to note here.

The role name and database name you are going to create must match the username and database names in the production settings written in your app's config/database.yml.



postgres=# CREATE ROLE "sample" WITH SUPERUSER LOGIN;

CREATE ROLE


postgres=# CREATE DATABASE "sample_production";

CREATE DATABASE


postgres=# \q


Finally, restart PostgreSQL

# systemctl restart postgresql.service


Web server and application server

1. Installation of the web server (Nginx)

Implement at EC2

$ sudo su -

# amazon-linux-extras install nginx1 -y


2. Autostart Settings and start confirmation

Implement at EC2

# chkconfig nginx on


Execute the following command to start Nginx

# systemctl start nginx