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

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

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

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

Q&A

解決済

1回答

1625閲覧

TypeError: no implicit conversion of nil into Stringについて

hanahana1

総合スコア3

Ruby

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

0グッド

0クリップ

投稿2023/02/09 09:50

実現したいこと

TypeError: no implicit conversion of nil into Stringエラーを解消したい。

前提

本番環境へseeds.rbのデータを反映させようと、rails db:seed RAILS_ENV=productionを入力したら、下記のエラーが出ました。

同様のエラー解決方法は参照しましたが、自分の状況に一致するものが分かりませんでした。
質問が重複しており申し訳ありませんが、ご助言いただけますと幸いです。

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

ユーザー@ユーザーMacBook-Air アプリ名 % rails db:seed RAILS_ENV=production rails aborted! TypeError: no implicit conversion of nil into String /Users/ユーザー/projects/アプリ名/bin/rails:9:in `<top (required)>' /Users/ユーザー/projects/アプリ名/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: PG::ConnectionBad: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory Is the server running locally and accepting connections on that socket? /Users/ユーザー/projects/アプリ名/bin/rails:9:in `<top (required)>' /Users/ユーザー/projects//bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Tasks: TOP => db:abort_if_pending_migrations (See full trace by running task with --trace)

該当のソースコード

bin/spring

1unless defined?(Spring) 2 require 'rubygems' 3 require 'bundler' 4 5 lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read) 6 spring = lockfile.specs.detect { |spec| spec.name == 'spring' } 7 if spring 8 Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path 9 gem 'spring', spring.version 10 require 'spring/binstub' 11 end 12end

bin/rails

1begin 2 load File.expand_path('../spring', __FILE__) 3rescue LoadError => e 4 raise unless e.message.include?('spring') 5end 6APP_PATH = File.expand_path('../config/application', __dir__) 7require_relative '../config/boot' 8require 'rails/commands'

seeds.rb

1User.create!(nickname: "admin", 2 email: "admin@admin", 3 password: "admin111", 4 password_confirmation: "admin111", 5 last_name: "山田", 6 first_name: "花子", 7 admin: true)

試したこと

bin/railsとbin/springという記述があったため、これらのファイルを確認しました。
他の方のエラーでは、①string型に変換できていないため.to_sをつける、②誤字があるといった解決方法がありました。

bin/railsとbin/springについて、アプリ作成段階から何も追記していないため、可能性があるとしたらseeds.rbかなと考えました。
nilをstring型に変えられないとあるため、どこか記入していない部分があるのかと思いましたが、全てのカラムが埋まっていました。
また、すべて文字列で記述したため、string型に変換できないという意味がよくわからず、ここで止まっています。

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

ここにより詳細な情報を記載してください。

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

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

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

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

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

otn

2023/02/09 12:30

> TypeError: no implicit conversion of nil into String それは結果であって、原因は、 > Caused by: と書いてある通り、下記ですね。 PG::ConnectionBad: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory で、 Is the server running locally and accepting connections on that socket? と、聞かれてますが、なんと答えますか?
guest

回答1

0

自己解決

otn様、ご意見ありがとうございました!

結論から言うと、質問したエラー内容は解決できませんでしたが、実現したかった挙動(本番環境のデータベースへseeds.rbのデータを反映させる)を render-build.shを下記のように編集することで実現しました。

ruby

1bundle install 2bundle exec rake assets:precompile 3bundle exec rake assets:clean 4DISABLE_DATABASE_ENVIRONMENT_CHECK=1 bundle exec rake db:migrate:reset 5bundle exec rake db:seed

render-build.shは、デプロイ時に自動で実行するコマンドを記述するファイルです。
そこに4・5行目を追記することで、デプロイ時に本番環境のデータベースをリセット&seeds.rbのデータを格納することが可能となりました。
これにより、当初質問していた下記のエラーは出なくなりましたので、自己解決とさせていただきます。
PG::ConnectionBad: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory

投稿2023/02/13 09:27

hanahana1

総合スコア3

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.30%

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

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

質問する

関連した質問