STUDY MEMO

学習のメモ書き

<Ruby on Rails エラー編> yarn install時のwarning ../../../package.json: No license field

発生したエラー

> yarn install
yarn install v1.22.10
warning ../../../package.json: No license field

指定のpackage.jsonにlicenseがない、ということでwarningが発生。
問題はないと思うが気持ち悪いので修正したいと思い、以下の対処方法を実施。

対処方法

指定されたpackage.jsonにlicenseを設定。

Finally, if you do not wish to grant others the right to use a private or unpublished package under any terms:

どんな時も他の人にprivateや未公表のパッケージを使用する権利を与えたくない場合は以下を使用するよう、
上記のように公式で定義されていたため、今回は以下のコードを記載。

{
  "license": "UNLICENSED"
}

yarn installするとwarningが消えていた。解決。

参考

package.json | npm Docs

<Ruby on Rails エラー編> bundle installで、Gem::Ext::BuildError: ERROR: Failed to build gem native extension.が発生

発生したエラー

> bundle
Warning: the running version of Bundler (2.0.2) is older than the version that created the lockfile (2.1.2). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`.
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`.
Fetching gem metadata from https://rubygems.org/.........
Using rake 13.0.1
Using concurrent-ruby 1.1.5
Using i18n 1.8.2
Using minitest 5.14.0
Using thread_safe 0.3.6
Using tzinfo 1.2.6
Using zeitwerk 2.2.2
Using activesupport 6.0.2.1
Using builder 3.2.4
Using erubi 1.9.0
Using mini_portile2 2.4.0
Fetching nokogiri 1.10.7
Installing nokogiri 1.10.7 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/nokogiri-1.10.7/ext/nokogiri
/Users/user_name/.rbenv/versions/2.6.5/bin/ruby -I
/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0 -r ./siteconf20210315-26465-xk69qq.rb
extconf.rb
checking if the C compiler accepts  -I
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/libxml2... *** extconf.rb
failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/Users/user_name/.rbenv/versions/2.6.5/bin/$(RUBY_BASE_NAME)
        --help
        --clean
/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:467:in `try_do': The compiler
failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:585:in `block in
try_compile'
        from /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:532:in `with_werror'
        from /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:585:in `try_compile'
        from extconf.rb:138:in `nokogiri_try_compile'
        from extconf.rb:162:in `block in add_cflags'
        from /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:643:in `with_cflags'
        from extconf.rb:161:in `add_cflags'
        from extconf.rb:416:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found
here:

/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-20/2.6.0/nokogiri-1.10.7/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/nokogiri-1.10.7 for
inspection.
Results logged to
/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-20/2.6.0/nokogiri-1.10.7/gem_make.out

An error occurred while installing nokogiri (1.10.7), and Bundler cannot
continue.
Make sure that `gem install nokogiri -v '1.10.7' --source 'https://rubygems.org/'`
succeeds before bundling.

In Gemfile:
  rails was resolved to 6.0.2.1, which depends on
    actioncable was resolved to 6.0.2.1, which depends on
      actionpack was resolved to 6.0.2.1, which depends on
        actionview was resolved to 6.0.2.1, which depends on
          rails-dom-testing was resolved to 2.0.3, which depends on
            nokogiri

Result

上記エラーで記載のあった、Result toの
/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-20/2.6.0/nokogiri-1.10.7/gem_make.outを確認。

current directory: /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/nokogiri-1.10.7/ext/nokogiri
/Users/user_name/.rbenv/versions/2.6.5/bin/ruby -I /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0 -r ./siteconf20210315-26465-xk69qq.rb extconf.rb
checking if the C compiler accepts  -I /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/libxml2... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/Users/user_name/.rbenv/versions/2.6.5/bin/$(RUBY_BASE_NAME)
    --help
    --clean
/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:467:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
    from /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:585:in `block in try_compile'
    from /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:532:in `with_werror'
    from /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:585:in `try_compile'
    from extconf.rb:138:in `nokogiri_try_compile'
    from extconf.rb:162:in `block in add_cflags'
    from /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/2.6.0/mkmf.rb:643:in `with_cflags'
    from extconf.rb:161:in `add_cflags'
    from extconf.rb:416:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-20/2.6.0/nokogiri-1.10.7/mkmf.log

extconf failed, exit code 1

mkmf.log

上記で記載のあった
/Users/user_name/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-20/2.6.0/nokogiri-1.10.7/mkmf.logを確認。

"clang -o conftest -I/Users/user_name/.rbenv/versions/2.6.5/include/ruby-2.6.0/x86_64-darwin20 -I/Users/user_name/.rbenv/versions/2.6.5/include/ruby-2.6.0/ruby/backward -I/Users/user_name/.rbenv/versions/2.6.5/include/ruby-2.6.0 -I. -I/Users/user_name/.rbenv/versions/2.6.5/include  -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT   -O3 -ggdb3 -Wall -Wextra -Wdeclaration-after-statement -Wdeprecated-declarations -Wdivision-by-zero -Wimplicit-function-declaration -Wimplicit-int -Wpointer-arith -Wshorten-64-to-32 -Wwrite-strings -Wmissing-noreturn -Wno-constant-logical-operand -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-parentheses-equality -Wno-self-assign -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-value -Wunused-variable -Wextra-tokens  -fno-common -pipe  -I /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/libxml2 conftest.c  -L. -L/Users/user_name/.rbenv/versions/2.6.5/lib -L. -L/Users/user_name/.rbenv/versions/2.6.5/lib  -fstack-protector-strong -L/usr/local/lib     -lruby.2.6    "
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
CommandLineToolsのパスが無効になってるということは、CommandLineToolsがinstallされてない可能性がある?
以下の対処法を実施。

対処方法

以下のコマンドでxcodeをinstallする。

$ xcode-select --install

余談

xcodeがインストールされて上記のコマンドを実行すると、以下の表示が出る。

$ xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates

<Zoom> ミーティングチャットのテキスト自動保存設定

設定

https://zoom.us/にログインし、マイアカウント->設定を選択

ミーティングにて(基本)のチャット自動保存をオンにする。
Image from Gyazo

保存場所の設定

zoom.usアプリを開く

歯車マークの設定を選択

レコーディングを選択

レコーディングの保存場所に保存したい場所を選択

これでテキストタイプで自動保存が出来るようになる。

<VSC> 末尾の空欄を保存時に削除する設定

設定方法

Command + ,を押してSettingsを開く

Search settingsに"files.trimTrailingWhitespace"と入力

チェックをつける
Image from Gyazo

command + shift + pでreload windowを選択し、再起動する。

参考

VS Codeで行末やファイル末尾の空白文字を削除するには:Visual Studio Code TIPS - @IT

<Ruby on Rails> gem 'rack-mini-profiler'について

rack-mini-profiler

RoRのパフォーマンス計測をするgem。
公式では以下のように記載されている。

Middleware that displays speed badge for every html page. Designed to work both in production and in development.
GitHub - MiniProfiler/rack-mini-profiler: Profiler for your development and production Ruby rack apps.

htmlページごとにスピードバッジを表示するミドルウェアで、本番環境でも開発環境でも動作するように設計されている。

ミドルウェア(Middleware)とは?
コンピュータの分野で、コンピュータの基本的な制御を行うオペレーティングシステム(OS)と、各業務処理を行うアプリケーションソフトウェアとの中間に入るソフトウェアのこと。
ミドルウェア - Wikipedia

使い方

インストール

Gemfileに以下を記載し、bundle install。
rails 6ではdevelopmentに標準装備されていた。

gem 'rack-mini-profiler'

非表示設定

option + pを押す。

<Ruby on Rails エラー編> Rack app ("GET /packs/js/application-f826770d917d8a37c1d8.js" - (::1))

rails sで以下のエラーが発生。

2021-03-07 02:36:40 +0900 Rack app ("GET /packs/js/application-f826770d917d8a37c1d8.js" - (::1)): #<Net::ReadTimeout: Net::ReadTimeout>
2021-03-07 02:36:40 +0900 Rack app ("GET /packs/js/application-f826770d917d8a37c1d8.js" - (::1)): #<Net::ReadTimeout: Net::ReadTimeout>
2021-03-07 02:36:40 +0900 Rack app ("GET /packs/js/application-f826770d917d8a37c1d8.js" - (::1, ::1)): #<Net::ReadTimeout: Net::ReadTimeout>
2021-03-07 02:36:40 +0900 Rack app ("GET /packs/js/application-f826770d917d8a37c1d8.js" - (::1, ::1, ::1)): #<Net::ReadTimeout: Net::ReadTimeout>
2021-03-07 02:36:40 +0900 Rack app ("GET /packs/js/application-f826770d917d8a37c1d8.js" - (::1, ::1, ::1, ::1)): #<Net::ReadTimeout: Net::ReadTimeout>
Started GET "/" for ::1 at 2021-03-07 02:36:40 +0900
Processing by TasksController#index as HTML
  Rendering layout layouts/application.html.slim
  Rendering tasks/index.html.slim within layouts/application
  Rendered tasks/index.html.slim within layouts/application (Duration: 0.1ms | Allocations: 37)
  Rendered layout layouts/application.html.slim (Duration: 26.5ms | Allocations: 6609)
Completed 200 OK in 31ms (Views: 29.7ms | ActiveRecord: 0.0ms | Allocations: 7673)
application-f826770d917d8a37c1d8.js:1 Failed to load resource: the server responded with a status of 500 (Internal Server Error)

結論

以下のコマンドを打ち忘れてたので、webpackerで使用される設定ファイルやディレクトリなどが生成されてなかった。

$ rails webpacker:install

git statusで変更されてるファイルを見たところ以下のファイルで色々怒っている様子。 貼ろうとしたけどえぐい量だったので省略。

modified:   config/webpacker.yml
modified:   package.json
modified:   yarn.lock

<Ruby on Rails エラー編> Webpacker::Manifest::MissingEntryError

rails6でrails sをしたところ、以下のエラーが発生した。

Webpacker::Manifest::MissingEntryError Tasks#index

# エラー発生場所 application.html.slim
= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload'

Webpacker can't find application.js in D:/ROR/blog/public/packs/manifest.json. Possible causes:
1. You want to set webpacker.yml value of compile to true for your environment
   unless you are using the `webpack -w` or the webpack-dev-server.
2. webpack has not yet re-run to reflect updates.
3. You have misconfigured Webpacker's config/webpacker.yml file.
4. Your webpack configuration is not creating a manifest.
Your manifest contains:
{
}


原因:javascript_pack_tagで読み込もうとしている対象のJavaScriptマニフェストがない

講師によると、

Rails6ではJavaScriptのパッケージマネージャをyarnで管理するようになったので、yarn add @rails/webpackerマニフェストファイル作成用のwebpackerをインストール対象に追加して、インストールしてファイルが作成されてエラーが解消されたという形になりますね。
yarn addを実行すると、package.jsonというJavaScriptのパッケージ(ソースコード、プログラムの集合)一覧にwebpackerの記載が追記されたかと思います。
Rails5系では、上記のような対応はデフォルトでは必要ないので、Rails6系特有の環境設定作業だった、ということになります。

とのこと。



ちなみに私の場合こちらを試したら解決。
Webpacker can't find application.js in D:/ROR/blog/public/packs/manifest.json in Ruby on Rails 6.1 · Issue #2825 · rails/webpacker · GitHub
こちらで解決できた方もいるらしい。
webpack - Rails Webpacker compile error on Production enviorment - Stack Overflow

<homebrew> nodenvを使用したNode.jsのバージョン管理

Node.jsとは

公式には以下のような記載がある。

Node.js はスケーラブルなネットワークアプリケーションを構築するために設計された非同期型のイベント駆動の JavaScript 環境です。
Node.js とは | Node.js

要はjavascriptの実行環境のこと?

スケラーブル(scalable)とは?
拡張可能な、拡張性のあるという意味。 リソースの量に比例して全体のスループットが向上するシステムはスケーラブルな(scalable)システムまたはスケーラビリティのあるシステムと呼ばれる。 スケーラブルとは - Weblio辞書
スループットとは?
コンピュータやネットワーク機器が単位時間あたりに処理できるデータ量のこと。
スループットとは - 意味の解説|ITトレンドのIT用語集

安定版について

Node.jsのリリースについては以下の3つのフェーズがある。
「最新」「LTS(active long term support」「メンテナンス」 奇数番号はLTSとして昇格されることはない、とあるので、基本的には偶数バージョンを使うのが良さそう。

nodenvとは

node.jsの管理ツール。

よく使うであろうコマンド

# バージョン確認
$ node 

# インストールできるバージョン一覧
$ nodenv install -l(または--list)

# 指定のバージョンのインストール
$ nodenv install [バージョン]

# インストール済みバージョンの確認
$ nodenv versions

# グローバルで使うnodeのバージョン指定
$ nodenv global [バージョン]

# プロジェクトでのみ使用するnodeのバージョン指定
$ nodenv local [バージョン]

参考資料

Node.jsって何ですか? - Qiita
nodebrewによるNode.jsのインストールからセットアップまで - Qiita
GitHub - nodejs/Release: Node.js Release Working Group
https://jsstudy.hatenablog.com/entry/Difference-in-version-of- Nodejs#%E5%AE%89%E5%AE%9A%E7%89%88LTS
GitHub - nodenv/nodenv: Manage multiple NodeJS versions.

<VSC> open a folder or workspace live server...というエラーがでた時の対処法

エラー内容

VScode拡張機能「live server」を使用し、リロード不要でgoogle chromeを使用したかったが、
Open with live serverをクリックしたら以下のエラーがでた。

open a folder or workspace live server...


対処法

以下のサイトを参照し、フォルダの中にブラウザに出したかったファイルを入れて、
command + Oで作成したフォルダを開いて、ブラウザに表示したかったhtml上でOpen with live serverを選択。 Visual Studio Code の Live Server で 「open a folder or workspace live server...」が出たときの 対処法 ~画像付き解説~ - Qiita

Image from Gyazo


メモ

Githubを見たところ、folderの中に作らないと認識しないっぽい。 vscode-live-server/faqs.md at master · ritwickdey/vscode-live-server · GitHub

<Ruby on Rails エラー編> ActiveRecord::RecordInvalid: Validation failed: Email has already been takenエラー

発生したエラー

rails db:seedでFactoryBotのデータを作成しようとしたところ、

rails aborted!
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
...

といったエラーが出た。

考えたこと

Raised by ActiveRecord::Base#save! and ActiveRecord::Base#create! when the record is invalid. Use the record method to retrieve the record which did not validate.
ActiveRecord::RecordInvalid

未確認のレコードを取得するためのレコードメソッドを使ってください、ということは、確認されたレコードがある=データがあるということ?
emailはuniquenessで定義されているので、一意でなければならないということを考えると、factorybotの前のデータがすでに入っていて、データが重複してる可能性がある。

対処法

データを消去してもう一度rails db:seedを実行したところ、エラーが消えた。

$ rails db:migration:reset

$ rails db:seed


参考

Ruby on Rails - Email has already been takenと出てテストがパスしない|teratail

<MAC関連> ショートカットキーの作成

「ユーザー登録を編集..」をショートカットとして登録したいと思って調べたところ、以下の記事を発見。
Macのユーザ辞書をショートカットで開く方法 | iPhoneアプリ個人開発とライフハック

使用したアプリケーション

Automator(すでにインストールされている)

方法

spotlightでアプリケーションを検索

Automatorを検索して、アプリケーションを開く。

クイックアクションを選択

Image from Gyazo

AppleScriptを実行をドロップ

アクションの検索窓で「AppleScriptを実行」を検索し、右へドロップ。 Image from Gyazo

scriptを入力

以下のscriptを入力し、わかりやすい名前で保存(commend + S)。

tell application "System Preferences"
 reveal anchor "Text" of pane id "com.apple.preference.keyboard"
 activate
end tell


ショートカット登録

システム環境設定=>キーボード=>ショートカットを開き、
サービス項目を開くと、下の方に一般項目が追加され、登録した名前で指定の挙動が登録されているので、自分でわかるようにショートカットを登録する。

<Ruby on Rails> rubyのバージョン変更

gemfileのなかのバージョンが、自分の環境のバージョンと異なっていた。
そのため、指定アプリケーション内で使うバージョンを指定した。

$ bundle
Your Ruby version is 2.6.6, but your Gemfile specified 2.6.4


1. rubyのバージョン確認

$ rbenv versions
  system
  2.3.1
  2.6.3
  2.6.4
  2.6.5
* 2.6.6 (set by /Users/user_name/workspace/app/.ruby-version)


2. localでのrubyバージョンの指定

$ rbenv local 2.6.4

$ rbenv versions   
  system
  2.3.1
  2.6.3
* 2.6.4 (set by /Users/user_name/workspace/app/.ruby-version)
  2.6.5
  2.6.6


参考

Rubyのバージョン変更 - Qiita

<Ruby on Rails> rails serverが切れないときの対処法

rails serverを起動しようとしたら、以下のようなメッセージがでた。

A server is already running. 〜〜〜

閉じるとき不具合があったのか、立ちっぱなしになっているみたいだったので、こちらの記事を参考に対処。

rails sをしたらtmp/pids/server.pidというエラーが出る件について - Qiita

tmp/pids/server.pidのなかにある数字を削除

Image from Gyazo

実行中のプロセスを確認

以下のコマンドで実行しているポート番号を探す。

$ lsof -i:3000

lsof:オープンしているファイルを表示
-i:ポート番号:ポート番号を指定
【 lsof 】コマンド――オープンしているファイルを一覧表示する:Linux基本コマンドTips(298) - @IT

結果、tmp/pids/server.pidと同じPIDの処理が実行中ということがわかる。
Image from Gyazo

プロセス強制終了

以下のコマンドでプロセスを終了させる。

$ kill pid番号

メモ

試しにkillの作業だけやってみたら対象のtmp/pids/server.pidファイルがなくなっていた。

<Mysql> 操作方法メモ

データベース

新しいデータベースを作成する

データベースの重複を防ぐため、if not existsを記載すること。

mysql> create database if not exists データベース名;

データベースを削除する

削除する前に作成済みのDBの一覧を確認する。

mysql> show databases;

削除する

mysql> drop database データベース名;

テーブル

テーブルを作成する

まず、テーブルを作るデータベースにアクセスする。

mysql> use データベース名;

テーブル名を記載し、()に作成したいカラムの名前とデータタイプを入力する。

mysql> create table テーブル名(
    -> id int(10),
    -> name varchar(255),
    -> date datetime);

テーブルの中身を確認

show columns from テーブル名 from データベース名;