Rails ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage

新規のアプリデプロイ時にこんなエラーが

-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       rake aborted!
       ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage
       /tmp/build_120aa013/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4.1/lib/active_support/message_encryptor.rb:203:in `rescue in _decrypt'
       /tmp/build_120aa013/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4.1/lib/active_support/message_encryptor.rb:180:in `_decrypt'
       /tmp/build_120aa013/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4.1/lib/active_support/message_encryptor.rb:154:in `decrypt_and_verify'
       /tmp/build_120aa013/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4.1/lib/active_support/messages/rotator.rb:22:in `decrypt_and_verify'
       /tmp/build_120aa013/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4.1/lib/active_support/encrypted_file.rb:92:in `decrypt'
       /tmp/build_120aa013/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4.1/lib/active_support/encrypted_file.rb:54:in `read'

マスターキーが正しくないことが原因のよう確認して環境変数 RAILS_MASTER_KEY に入れ直して解消

cat config/master.key

Heroku Railsアプリデプロイ時にrake assets:precompileでNoMethodError: undefined method `[]' for nil:NilClass

久しぶりに rails new して Herokuにデプロイした時にエラー

-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       rake aborted!
       NoMethodError: undefined method `[]' for nil:NilClass

master.keyをHeroku環境変数に設定する

master.keyが登録されていないことが原因でした。 以下のコマンドで解決

$ heroku config:set RAILS_MASTER_KEY=`cat config/master.key`

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に大人しくなってもらいました。