rails credentials:editでNo $EDITOR to open file in. Assign one like this:と怒られる

$ rails credentials:edit
No $EDITOR to open file in. Assign one like this:

EDITOR="mate --wait" bin/rails credentials:edit

For editors that fork and exit immediately, it's important to pass a wait flag,
otherwise the credentials will be saved immediately with no chance to edit.

EDITORが未指定の場合は編集することができないとのこと

解決策

エディタを指定して実行

$ EDITOR="vi" bin/rails credentials:edit

EDITORを環境変数に設定しておく

環境変数に設定しておけば次回以降は rails credentials:edit だけで編集可能です。

echo 'export EDITOR="vi"' >> ~/.bash_profile

Rspecで WARNING: Using `expect { }.not_to raise_error(SpecificErrorClass)` risks false positives

Rspec実行時に以下で怒られました。

WARNING: Using `expect { }.not_to raise_error(SpecificErrorClass)` risks false positives, since literally any other error would cause the expectation to pass, including those raised by Ruby (e.g. `NoMethodError`, `NameError` and `ArgumentError`), meaning the code you are intending to test may not even get reached. Instead consider using `expect { }.not_to raise_error` or `expect { }.to raise_error(DifferentSpecificErrorClass)`. This message can be suppressed by setting: `RSpec::Expectations.configuration.on_potential_false_positives = :nothing`.

雑にGoogle翻訳する

警告: expect {} .not_to raise_error(SpecificErrorClass)を使用すると、誤検知のリスクがあります。これは、Rubyによって発生したエラー(例: NoMethodErrorNameErrorArgumentError)を含め、文字通り他のエラーが期待を通過させるためです。つまり、テストしようとしているコードに到達できない可能性があります。代わりに、 expect {} .not_to raise_errorまたはexpect {} .to raise_error(DifferentSpecificErrorClass)の使用を検討してください。このメッセージは、 RSpec :: Expectations.configuration.on_potential_false_positives =:nothingを設定することで抑制できます。

expect {}.not_to raise_error (SpeificErrorClass) を使うと、該当エラーが起こる前に別エラーが投げられた場合などで通ってしまう誤検知の可能性があるらしい。
そのためエラーが起こらないことのテスト expect {}.not_to raise_error とするか、別のエラーが起こるテスト expect {} .to raise_error (DifferentSpecificErrorClass) にしましょうとのこと。
今回はクラスを指定せず、エラーが起こらないことを確認するテストに書き換えてRspecに大人しくなってもらいました。

Ruby HTTPClientでpostする

簡単なことなんだけどbodyを入れるのがうまくいかず詰まったのでメモ

require 'httpclient'
require 'json'

client ||= ::HTTPClient.new

# body を jsonにする必要がある
body = JSON.generate({ hoge: { moge: 1234 } })
# railsの場合は to_json で ok

header = {
  'Content-Type' => 'application/json'
}

client.post "https://example.com/api/hoge/moge", body: body, header: header

docker-compose で Can't separate key from value と怒られた時の対応

Docker for Desktopをアップデートしたら以下のエラーが

$ docker-compose run
=> Can't separate key from value

Docker2をデフォルトで使う設定になってしまっているようなのでチェックを外し設定をオフにします。

f:id:kuronekopunk:20210728062017p:plain

stackoverflow.com

Rails consoleでrake taskを呼び出す

デバッグバッチ処理内でRake taskを呼び出したいときの対応

Rails.application.load_tasks
Rake::Task['my_task'].execute
# 引数を渡す場合
Rake::Task['my_task'].execute('arg_string')
Rake::Task['my_task'].execute(hoge: 'moge')

my_task には hoge:moge など rake hoge:moge と指定する名前が入ります。
他サンプルだと require 'rake' しているものもありますが rails c で入っている分には不要でした。

stackoverflow.com