質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

966閲覧

ローカル環境でRails s, Rails g, Rails cが使えなくなってしまった

hasaya

総合スコア4

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/06/29 05:41

前提・実現したいこと

Railsの初学者です。現在開発中のアプリをローカル環境で動かしながら作業してしていたところ、rails sコマンドが使えなくなりました。またrails gやrails cも使えなくなっておりエラー文の

(erb):2:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)

部分を見る限りだとどこが原因となっているのかを自分では特定出来ず調べたりしたのですが、原因が分からずそこから詰まっております。恐れ入りますがどこに解決の糸口があるのかアドバイスをいただけると幸いです。

発生している問題・エラーメッセージ

rails s 実行時(rails g,rails cコマンドでも同様のエラーです) (erb):2:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError) from /Users/.rbenv/versions/3.0.0/lib/ruby/3.0.0/erb.rb:905:in `eval' from /Users/.rbenv/versions/3.0.0/lib/ruby/3.0.0/erb.rb:905:in `result' from /Users/vendor/bundle/ruby/3.0.0/gems/config-3.1.0/lib/config/sources/yaml_source.rb:19:in `load' from /Users/vendor/bundle/ruby/3.0.0/gems/config-3.1.0/lib/config/options.rb:38:in `block in reload!' from /Users/vendor/bundle/ruby/3.0.0/gems/config-3.1.0/lib/config/options.rb:37:in `each' from /Users/vendor/bundle/ruby/3.0.0/gems/config-3.1.0/lib/config/options.rb:37:in `reload!' from /Users/vendor/bundle/ruby/3.0.0/gems/config-3.1.0/lib/config.rb:48:in `load_files' from /Users/vendor/bundle/ruby/3.0.0/gems/config-3.1.0/lib/config.rb:56:in `load_and_set_settings' from /Users/vendor/bundle/ruby/3.0.0/gems/config-3.1.0/lib/config/integrations/rails/railtie.rb:11:in `preload' from /Users/vendor/bundle/ruby/3.0.0/gems/railties-6.1.3.1/lib/rails/railtie.rb:207:in `public_send' from /Users/vendor/bundle/ruby/3.0.0/gems/railties-6.1.3.1/lib/rails/railtie.rb:207:in `method_missing' from /Users/vendor/bundle/ruby/3.0.0/gems/config-3.1.0/lib/config/integrations/rails/railtie.rb:21:in `block in <class:Railtie>' from /Users/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/lazy_load_hooks.rb:68:in `block in execute_hook' from /Users/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/lazy_load_hooks.rb:61:in `with_execution_control' from /Users/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/lazy_load_hooks.rb:66:in `execute_hook' from /Users/hayat/voice_component/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/lazy_load_hooks.rb:52:in `block in run_load_hooks' from /Users/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/lazy_load_hooks.rb:51:in `each' from /Users/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/lazy_load_hooks.rb:51:in `run_load_hooks' from /Users/vendor/bundle/ruby/3.0.0/gems/railties-6.1.3.1/lib/rails/application.rb:96:in `inherited' from /Users/config/application.rb:24:in `<module:VoiceComponent>' from /Users/config/application.rb:22:in `<main>' from /Users/vendor/bundle/ruby/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' from /Users/vendor/bundle/ruby/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' from /Users/vendor/bundle/ruby/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' from /Users/hayat/voice_component/vendor/bundle/ruby/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' from /Users/vendor/bundle/ruby/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' from /Users/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:332:in `block in require' from /Users/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:299:in `load_dependency' from /Users/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:332:in `require' from /Users/vendor/bundle/ruby/3.0.0/gems/railties-6.1.3.1/lib/rails/commands/server/server_command.rb:138:in `block in perform' from <internal:kernel>:90:in `tap' from /Users/vendor/bundle/ruby/3.0.0/gems/railties-6.1.3.1/lib/rails/commands/server/server_command.rb:135:in `perform' from /Users/vendor/bundle/ruby/3.0.0/gems/thor-1.1.0/lib/thor/command.rb:27:in `run' from /Users/vendor/bundle/ruby/3.0.0/gems/thor-1.1.0/lib/thor/invocation.rb:127:in `invoke_command' from /Users/vendor/bundle/ruby/3.0.0/gems/thor-1.1.0/lib/thor.rb:392:in `dispatch' from /Users/vendor/bundle/ruby/3.0.0/gems/railties-6.1.3.1/lib/rails/command/base.rb:69:in `perform' from /Users/vendor/bundle/ruby/3.0.0/gems/railties-6.1.3.1/lib/rails/command.rb:50:in `invoke' from /Users/vendor/bundle/ruby/3.0.0/gems/railties-6.1.3.1/lib/rails/commands.rb:18:in `<main>' from /Users/vendor/bundle/ruby/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' from /Users/vendor/bundle/ruby/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' from /Users/vendor/bundle/ruby/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' from /Users/hayat/voice_component/vendor/bundle/ruby/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' from /Users/vendor/bundle/ruby/3.0.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' from /Users/bin/rails:5:in `<top (required)>' from /Users/vendor/bundle/ruby/3.0.0/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `load' from /Users/vendor/bundle/ruby/3.0.0/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `call' from /Users/vendor/bundle/ruby/3.0.0/gems/spring-2.1.1/lib/spring/client/command.rb:7:in `call' from /Users/vendor/bundle/ruby/3.0.0/gems/spring-2.1.1/lib/spring/client.rb:30:in `run' from /Users/vendor/bundle/ruby/3.0.0/gems/spring-2.1.1/bin/spring:49:in `<top (required)>' from /Users/vendor/bundle/ruby/3.0.0/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `load' from /Users/vendor/bundle/ruby/3.0.0/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `<top (required)>' from <internal:/Users/.rbenv/versions/3.0.0/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require' from <internal:/Users/.rbenv/versions/3.0.0/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require' from /Users/bin/spring:10:in `block in <top (required)>' from <internal:kernel>:90:in `tap' from /Users/hayat/voice_component/bin/spring:7:in `<top (required)>' from bin/rails:2:in `load' from bin/rails:2:in `<main>'

該当のソースコード

こちらが原因ではないかもしれませんが、補足情報に載せている参考記事にdatabase.ymlが原因ではないかとの記載があったのでこちらを載せます。

ソースコード config/database.yml 2 default: &default 3 adapter: mysql2 4 encoding: utf8mb4 5 charset: utf8mb4 6 collation: utf8mb4_general_ci 7 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 8 username: root 9 password: 10 socket: /tmp/mysql.sock 11 12 development: 13 <<: *default 14 database: Sample_development 15 16 test: 17 <<: *default 18 database: Sample_test 19 20 production: 21 <<: *default 22 database: <%= Rails.application.credentials.db[:database] %> 23 username: <%= Rails.application.credentials.db[:username] %> 24 password: <%= Rails.application.credentials.db[:password] %> 25 host: <%= Rails.application.credentials.db[:host] %> 26

試したこと

$ gem install bundler

を実行し、その後下記を実行

$ bundle install --path vendor/bundle

補足情報(FW/ツールのバージョンなど)

Rails 6.1.3.1 ruby 3.0.0

調べていて似たようなエラーの参考記事があったのですが、これが自分の中で上手く理解しきれておらず、まだこちらは試しておりません。
https://stackoverflow.com/questions/65496869/erb48in-main-undefined-method-for-nilnilclass-nomethoderror

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

database: <%= Rails.application.credentials.db[:database] %>
username: <%= Rails.application.credentials.db[:username] %>
password: <%= Rails.application.credentials.db[:password] %>
host: <%= Rails.application.credentials.db[:host] %>

これらは、Rails.application.credentials.dbが読み取れる事を期待しています。
そのためにはmaster.keyもしくは環境変数RAILS_MASTER_KEYが必要です。

あなたにmaster.keyにアクセスする権限がある場合は設定すりゃいいですが
権限がない場合、もしくは管理に自信がない場合には以下2つの道があります。

  1. database.ymlを都度変更し以上の4行を消し、git commitしないようにexcludeにdatabase.ymlを登録しておく
  2. database.yml

yaml

1database: <%= Rails.application.credentials.dig(:db, :database) %> 2username: <%= Rails.application.credentials.dig(:db, :username) %> 3password: <%= Rails.application.credentials.dig(:db, :password) %> 4host: <%= Rails.application.credentials.dig(:db, :host) %>

に書き換え、権限のある人にテストしてもらいgitにもcommit/pushする。

投稿2021/06/29 07:27

asm

総合スコア15147

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hasaya

2021/06/29 08:51 編集

回答ありがとうございます。以前ローカルにてmaster.keyの存在、中身も確認しており、問題なく使えておりました。 その後git pushして本番環境でもmaster.keyを使用して問題なく使えておりましたが、今ローカル環境のconfig以下を確認したところ、master.keyがなくなっておりました。(git pushした際に消失することがあるのでしょうか?) その為、ローカルでブランチを切り替えてmaster.keyを作成し、本番環境に残っていたmaster.keyの内容をコピーして保存。その後rails sを実行したところ今度は下記のエラーが発生しております。 vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.3.1/lib/active_support/message_encryptor.rb:203:in `rescue in _decrypt': ActiveSupport::MessageEncryptor::InvalidMessage (ActiveSupport::MessageEncryptor::InvalidMessage) 下記記事をもとに新しく生成された鍵は既存のconfig/credentials.yml.encの復号には使えない ということが記載があるということはこれはもう使えないということでしょうか? [参考](https://qiita.com/suzy1031/items/64598d44b428ccd7abc5)
asm

2021/06/29 09:01

> git pushした際に消失することがあるのでしょうか? git reset --hardやブランチ切り替えなどで消失した可能性があります > これはもう使えない master.keyを生成し本番環境からコピーというのが何をしたかはわかりかねますが 本番環境からconfig/credentials.yml.encとmaster.key両方をローカルにコピーするとよいと思います。
hasaya

2021/06/30 00:28

返答遅くなり申し訳ありません。 ローカルにconfig/credentials.yml.encは残っていたので、master.keyだけコピーしてrails sを実行してみましたが同様のエラーが出てしまいました。やり方が間違っているのでしょうか?何度も申し訳ありません。
asm

2021/06/30 00:56

> ローカルにconfig/credentials.yml.encは残っていたので 正直、何やってローカルのmaster.keyが紛失しcredential.yml.encと相違が発生したのかわからない上に そもそも、credentials.yml.encが復号できない事が問題なのかも釈然としないので 原因としてcredentials.yml.encを排除するために両方のコピーもしくは新規作成をおすすめします。
hasaya

2021/06/30 01:25

新しいブランチを作成してそこでcredentials.yml.encとmaster.keyを一旦別ファイルに移動させ、そこで bundle exec rails credentials:edit を実行して新規作成を試みましたがやはり同様のエラーが出てきます。ということは別の原因があるということですね・・・
asm

2021/06/30 04:18

この回答で示したようにdatabase.ymlを編集しても問題が発生するというのであれば まあ別なんでしょうね。 from /Users/.rbenv/versions/3.0.0/lib/ruby/3.0.0/erb.rb:905:in `result' from /Users/vendor/bundle/ruby/3.0.0/gems/config-3.1.0/lib/config/sources/yaml_source.rb:19:in `load' がある事からどこかのymlファイルのerb部分で問題が発生しているようですが 残念ながら現状のconfig gemではエラーが発生したymlファイルを特定することはできず git diffなどを頼りに何が原因かを探る事になります。 邪道としては /Users/vendor/bundle/ruby/3.0.0/gems/config-3.1.0/lib/config/sources/yaml_source.rb:19:in `load' の直前にSTDERR.puts @pathを挿入します
hasaya

2021/06/30 05:09

ありがとうございます!また分からないなりにご教示いただいた方法で原因探っていきたいと思います。お忙しいところお付き合いいただきありがとうございました!
guest

0

自己解決

こちらmaster.keyをもう一度よく確認したところ改行が入っており、そこを削除したところ無事に復旧いたしました。

投稿2021/07/01 23:48

hasaya

総合スコア4

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問