エラーの園

仕事、日常のあらゆるエラーと戦い、先人の知恵も拝借して何としても解決……したい!

NoMethodError: undefined method `build' for〜

エラー発生前の操作や設定など

FactoryBot使いたくて、Gemfileに定義して、bundle installした。
その後、下記の設定をしたが、上記エラーが出た。
spec/rails_helper.rb

Shoulda::Matchers.configure do |config|  
  config.integrate do |with|  
    with.test_framework :rspec  
    with.library :rails  
  end  
end  

エラー内容

NoMethodError: undefined method `build' for #<RSpec::ExampleGroups::[model名]:0x00007fc3f055b7a0>  

参考サイト

https://qiita.com/key_it6/items/fb94f229e9e45270c515

ありがとうございます!無事、解決しました🙇🏻‍♀️

原因

エラー発生前の操作や設定など以外にも設定が必要だった。
解決手順参照。

作業環境

$ sw_vers  
ProductName:    Mac OS X  
ProductVersion: 10.15.4    

解決手順

1.'spec/support/factory_bot.rb'を作成する。

mkdir spec/support  
touch spec/support/factory_bot.rb  

2.1.で作成したspec/support/factory_bot.rbに下記を記述する。

RSpec.configure do |config|  
  config.include FactoryBot::Syntax::Methods  
end  

3.spec/rails_helper.rbspec/support/factory_bot.rbをrequireする。

require 'support/factory_bot'  

ssh: connect to host [IPアドレス] port [ポート番号]: connection refused

エラー発生前の操作や設定など

ssh -p 10022 [user_id]@[LinuxマシーンのIPアドレス]
を実行した。

エラー内容

ssh: connect to host [IPアドレス] port 10022: connection refused  

参考サイト

1.stackoverflow#15118

ja.stackoverflow.com
ありがとうございます!助かりました🙇🏻‍♀️

2.ubuntu server での SSH の設定方法

yamamo10.jp
ありがとうございます!詳しい解説、助かります🙇🏻‍♀️
今後も、活用させていただきます😊

原因

どこかのサイトでglobal_IPアドレスを指定していたのを、そのまま鵜呑みにして実施していたため。
。。。必死になってglobal_IPアドレスに対してping叩いていた。
よくよく考えたら同じLan内のマシーンに接続するからglobal_IPアドレスは変だ💦
すぐに気がつかなかった自分が恥ずかしい😖

作業環境

$ sw_vers  
ProductName:    Mac OS X  
ProductVersion: 10.15.4  
OS:"Ubuntu"
VERSION:"14.04.6 LTS, Trusty Tahr"

解決手順

1.参考サイト1のunaristさんの回答に記載されていた確認項目を確認した。

※1−1〜4は参考サイトのunaristさんの回答を引用。

1−1.IPアドレスが間違っていて、別のPCを指している。

global_IPアドレスにこだわりすぎて、あれこれ確認したが、
一旦中断して別の用事をしている時に、不意に「なんでglobal_IPアドレス?」と気がつきIPアドレスで試した。

$ ssh -p 10022 [user_id]@[LinuxマシーンのIPアドレス]  
The authenticity of host '[LinuxマシーンのIPアドレス]:10022 ([192.168.0.5]:10022)' can't be established.  
ECDSA key fingerprint is SHA256:kC5VJXTWAIZC53kItuS0PrPi1fykF2mz2XSqdPVLhCA.  
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes  
Warning: Permanently added '[LinuxマシーンのIPアドレス]:10022' (ECDSA) to the list of known hosts.  
[user_id]@[LinuxマシーンのIPアドレス]'s password:  
Welcome to Ubuntu 14.04.6 LTS (GNU/Linux 3.13.0-170-generic i686)  
  
 * Documentation:  https://help.ubuntu.com/  
  
The programs included with the Ubuntu system are free software;  
the exact distribution terms for each program are described in the  
individual files in /usr/share/doc/*/copyright.  
  
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by  
applicable law.  
1−2.接続先で sshd が起動していない

1−1.で解決したので確認していないが、
今後のために、確認方法を下記に記載しておく。
※参考サイト2より引用

systemctl start ssh SSH のデーモンを起動します.
systemctl stop ssh SSH のデーモンを停止します.
systemctl restart ssh SSH デーモンを再起動します.デーモンが実行されていない場合,起動します.
systemctl reload ssh 設定ファイルを再読込みします.
systemctl status ssh ステータスを表示します.

1ー3.接続に使うポートが間違っている

確認したが、正しかった。

1−4.ファイアウォールによって通信が遮断されている

1−1.で解決したので未実施。

UbuntuのVimで矢印キーを押しても移動せず「ABCD」と入力されてしまう + Backspaceも効かない

エラー発生前の操作や設定など

Vimで該当ファイルを開いて、編集モード(i)にした後、
矢印キーで該当箇所に移動しようと「↑↓←→」などを押下した。
あれあれと思い、Backspaceも押下した。

エラー内容

UbuntuVimで矢印キーとBackspaceが想定外の挙動になった。

参考サイト

1.Ubuntuの矢印キー

qiita.com
ありがとうございます!
矢印キーとBackspace使える様になりました🙇🏻‍♀️

原因

参考サイトより、Vimの設定ファイルに矢印キーとBackspaceの設定をしていなかった。

作業環境

OS:"Ubuntu"  
VERSION:"14.04.6 LTS, Trusty Tahr"  

解決手順

1./home/[user_name]/直下に.vimrcファイルの存在確認をする。

$ ls -l ~/.vimrc  
ls: /home/kuroneko/.vimrc にアクセスできません: そのようなファイルやディレクトリはありません  
1−1.存在しなければ、.vimrcファイルを作成する。
$ touch ~/.vimrc  

2..vimrcファイルを編集する。

sudo vi ~/.vimrc  
set nocompatible  
set backspace=indent,eol,start  

E325: ATTENTION...

エラー発生前の操作や設定など

UbuntuVimエディターでSSHの設定ファイルを編集中、
矢印キーが想定通りに動作せず、強引にターミナルを閉じた。
別ターミナルで、sudo vi /etc/ssh/sshd_configを実行した。

エラー内容

E325: ATTENTION  
Found a swap file by the name "/etc/ssh/.sshd_config.swp"  
          owned by: root   dated: Wed May 13 18:45:30 2020  
         file name: /etc/ssh/sshd_config  
          modified: YES  
         user name: root   host name: kuroneko-PC-LL550VG1JR  
        process ID: 3438  
While opening file "/etc/ssh/sshd_config"  
             dated: Wed May 13 18:40:52 2020  
  
(1) Another program may be editing the same file.  If this is the case,  
    be careful not to end up with two different instances of the same  
    file when making changes.  Quit, or continue with caution.  
(2) An edit session for this file crashed.  
    If this is the case, use ":recover" or "vim -r /etc/ssh/sshd_config"  
    to recover the changes (see ":help recovery").  
    If you did this already, delete the swap file "/etc/ssh/.sshd_config.swp"  
    to avoid this message.  
"/etc/ssh/sshd_config" 88 lines, 2541 characters  
Press ENTER or type command to continue  

参考サイト

https://qiita.com/elu_jaune/items/1e81a650dec942f55c0b
ありがとうございます!助かりました🙇🏻‍♀️

原因

最初のターミナルが該当ファイルを掴んでいたのに、
強引に閉じて別ターミナルで同一ファイルを開こうとしたため。

参考サイトより一部抜粋

多重オープンしてしまった。

作業環境

OS:"Ubuntu"  
VERSION:"14.04.6 LTS, Trusty Tahr"  

解決手順

1.開きたいファイルのswapファイルができているとのことなので、swapファイルを確認。

$ sudo ls -la /etc/ssh  
合計 316  
drwxr-xr-x   2 root root   4096  5月 13 18:52 .  
drwxr-xr-x 132 root root  12288  5月 13 18:49 ..  
-rw-r--r--   1 root root  16384  5月 13 18:45 .sshd_config.swp  
  
...(中略)...  
  
-rw-r--r--   1 root root   2541  5月 13 18:40 sshd_config  

2.swapファイルを削除する。

$ sudo rm .sshd_config.swp  

3.確認として、開きたいファイルを開いてみる。

$ sudo vi /etc/ssh/sshd_config  

開きました!

# Package generated configuration file  
# See the sshd_config(5) manpage for details  
  
# What ports, IPs and protocols we listen for  
Port 10022  
# Use these options to restrict which interfaces/protocols sshd will bind to  
#ListenAddress ::  
#ListenAddress 0.0.0.0  

Sprockets::Rails::Helper::AssetNotPrecompiled

エラー発生前の操作や設定など

Javascriptを追加した後、ブラウザからlocalhost:3000にアクセスした。

エラー内容

Sprockets::Rails::Helper::AssetNotPrecompiled in Posts#index  
Showing /Users/[user_name]/Library/Mobile Documents/com~apple~CloudDocs/rails/[プロジェクト名]]/app/views/posts/index.html.erb where line #4 raised:  
  
Asset was not declared to be precompiled in production.  
Add `Rails.application.config.assets.precompile += %w( [追加したJavascript名].js )` to `config/initializers/assets.rb` and restart your server  

参考サイト

https://programming-beginner-zeroichi.jp/articles/213

ありがとうございます!助かりました🙇🏻‍♀️

原因

参考サイトの引用させていただきます。

本エラーは新規作成したJSファイルがプリコンパイルの対象から外れている為発生します。

作業環境

$ sw_vers  
ProductName:    Mac OS X  
ProductVersion: 10.15.4  

下記、メモが残っていなかったのでRubyrbenv versions
Railsはメモの時期でAboutに特定しました。

ruby 2.4.3  
rails 5.2.3  

解決手順

1.config/initializers/assets.rbに追加したJavascript名を追記する。

Rails.application.config.assets.precompile += %w( [追加したJavascript名].js )  

(例)追加したJavascriptのファイル名が pink_Gerbera.js の場合

Rails.application.config.assets.precompile += %w( pink_Gerbera.js )  

おまけ

エラー結構、ため込んでたつもりだったのに、
もう底を尽きそうだ。
日曜だけの練習じゃ時間が限られてるから、メモを残す暇も無かった。
新たなブログ「日曜ITエンジニア劇場」を開設して、
エラーの大量仕入れを狙おうと思う。
エラーって、仕事中は泣くほど嫌いだったのに、
今となっては、宝物だ👑金銀財宝、ざっくっ、ざっくぅ💰
そして、只今、毎日が「日曜ITエンジニア劇場」。。。 不景気、上等!かかってこんかーい!!

新ブログを開設したら、こちらで宣伝させていただきます🙇🏻‍♀️
お暇な時にお立ち寄りくださいませ😊

An error occurred while installing bcrypt...

エラー発生前の操作や設定など

https://railstutorial.jp/chapters/modeling_users
の「6.3.1 ハッシュ化されたパスワード」で
Gemfileにgem 'bcrypt', '3.1.12'を追記してbundle install実行。

エラー内容

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

参考サイト

1.解決手順そのまま引用させていただいてます。

qiita.com

ありがとうございます!助かりました🙇🏻‍♀️
gem install bcrypt --platform=rubyの実行でエラーが出たものの、
解決しました🙆‍♀️

2.x-code関連のエラー(規約の承認メッセージ)対応

qiita.com

ありがとうございました!
英語がいっぱい出てくると慌ててしまって、
「エラーだ!」と勘違いしたんですが、
規約の承認メッセージだったんですね。
助かりました🙇🏻‍♀️

3.GCC関連のエラー

qiita.com

ありがとうございます!助かりました🙇🏻‍♀️

原因

作業環境

$ sw_vers  
ProductName:    Mac OS X  
ProductVersion: 10.15.4  

下記、メモが残っていなかったのでRubyrbenv versions
Railsはメモを記述した時期を元にAboutに特定しました。

ruby 2.4.3  
rails 5.2.3  

解決手順

※1〜5の解決手順は、参考サイト1の解決策を引用してます。

1.gem uninstall bcrypt を実行

2.gem uninstall bcrypt-ruby を実行

3.gem install bcrypt --platform=rubyを実行

【補足】

X-Codeの規約の承認メッセージやGCC関連のエラーが出るかもしれない。

◆X-code

下記エラー内容、参考サイト2.の引用。

Agreeing to the Xcode/iOS license...  

これもメモにエラーが残っていない。
Railsの練習したくて慌ててたのだと思う😵💦

GCC

これもメモにエラーが残っていない。
参考サイト3.はHomebrewでGCCをインストールする内容なので、
恐らくどこかのサイトを調べてGCCが未インストールということに気づいたのだと思う。
gccに関するエラーが出たら、下記を試してインストールされているか確認する。
本エラーの対応したので、私のMacにはインストールされている。
下記の通り。

$ gcc -v  
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1  
Apple clang version 11.0.3 (clang-1103.0.32.59)  
Target: x86_64-apple-darwin19.4.0  
Thread model: posix  
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin  

インストールされていたら原因は他にあるかもしれません。
Googleさんにお尋ねくださいませ👂
健闘を祈る💪

4.Gemfileにgem 'bcrypt', '~> 3.1.11'を記載

5.bundle installを実行

`add_route': Invalid route name, already in use: [エラーになったroute name] (ArgumentError)

エラー発生前の操作や設定など

参考サイトの通り、設定してコマンドを実行した。
その後、サーバー起動(rails s)した。

エラー内容

config/routes.rb

Rails.application.routes.draw do  
  devise_for :admin_users, ActiveAdmin::Devise.config  
  devise_for :admin_users, ActiveAdmin::Devise.config  
  ActiveAdmin.routes(self)  
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html  
end  

devise_for :admin_users, ActiveAdmin::Devise.configが重複している。。。

参考サイト

本家Active Adminサイト(github)
↑のGetting started(入門)で紹介のあったドキュメント(doc)

原因

config/routes.rb

Rails.application.routes.draw do  
  devise_for :admin_users, ActiveAdmin::Devise.config  
  devise_for :admin_users, ActiveAdmin::Devise.config  
  ActiveAdmin.routes(self)  
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html  
end  

devise_for :admin_users, ActiveAdmin::Devise.configが重複していることが、
本エラーの原因だったが、なぜ重複したのかは不明。
参考サイト通り実施したつもりだったが、手順誤りだろうか。。。今となっては、検証できない。
(今後はサイトを参考に学習する際は、こまめにCommitして手順誤りを検証できるようにしようと思う←【教訓🧐】)
参考サイトの手順をやり直したら、下記のやり直し途中のエラー以外、
何も起こらずスムーズに作業が進んだ。

やり直し途中のエラー

再度、手順をやり直している際に、でBundleで管理しているlistenが
Gemfileの指定より新しいと叱られた。下記記事、参照。
The bundle currently has listen locked at [listenのバージョン] - エラーの園

作業環境

$ sw_vers  
ProductName:    Mac OS X  
ProductVersion: 10.15.4  
ruby 2.6.2p47  
Rails 6.0.3  

解決手順

1.config/routes.rbの重複した1行を削除(とりあえずコメントアウト)。

Rails.application.routes.draw do  
  devise_for :admin_users, ActiveAdmin::Devise.config  
  # devise_for :admin_users, ActiveAdmin::Devise.config  
  ActiveAdmin.routes(self)  
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html  
end  

2.サーバー起動

実行時ログ
$ rails s  
=> Booting Puma  
=> Rails 6.0.3 application starting in development  
=> Run `rails server --help` for more startup options  
Puma starting in single mode...  
* Version 4.3.3 (ruby 2.6.2-p47), 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  
Started GET "/" for ::1 at 2020-05-11 10:35:45 +0900  
   (2.5ms)  SELECT sqlite_version(*)  
   (1.6ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC  
Processing by Rails::WelcomeController#index as HTML  
  Rendering /Users/[user_name]/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/railties-6.0.3/lib/rails/templates/rails/welcome/index.html.erb  
  Rendered /Users/[user_name]/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/railties-6.0.3/lib/rails/templates/rails/welcome/index.html.erb (Duration: 12.6ms | Allocations: 315)  
Completed 200 OK in 46ms (Views: 31.2ms | ActiveRecord: 0.0ms | Allocations: 1708)  

正常に起動!