Railsアプリ作成するときに遭遇したエラーたち(随時追加、Docker環境含む)
- Ruby
- Rails環境構築・作成時のエラー
- Rails
- {:class=>"form-control"} is not a symbol nor a string
- ActionView::Template::Error - nil is not a valid asset source
- ActionView::Template::Error - undefined method `stringify_keys'
- ActionView::Template::Error at /xxx - undefined method `yyy' for nil:NilClass
- ActiveRecord::NoEnvironmentInSchemaError:
- Association named 'xxx' was not found on yyy; perhaps you misspelled it?
- Errno::EIO: Input/output error
- Routing Error(No route matches [GET] "/sign_out")
- Routing Error(No route matches [GET] "/xxx")
- SystemStackError at xxx stack level too deep
- Docker
- エラーではないけれど
Ruby
can't add a new key into hash during iteration
Railsのcontrollerでデータを加工しようとした時に発生したエラー。 eachで回してる最中に回してる配列をいじっちゃいけないみたいです(m m)
- 私は別にhashを宣言してそこに値をぶち込むことで解決しました(直接的な解決策ではないです)
下記のサイトはとても丁寧にエラーの原因が説明されています。
Rails環境構築・作成時のエラー
An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue.Make sure that gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'
succeeds before bundling.
rails new
もしくは bundle install
するときに下記のエラーが発生した
2 warnings generated. compiling infile.c compiling mysql2_ext.c compiling result.c compiling statement.c linking shared-object mysql2/mysql2.bundle ld: library not found for -lssl clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [mysql2.bundle] Error 1 make failed, exit code 2
- 私は【Rails】MySQL2がbundle installできない時の対応方法 - Qiitaで解決できた
- 下記がとても詳しく書いてある(mysql2 gemインストール時のトラブルシュート - Qiita)
Rails
{:class=>"form-control"} is not a symbol nor a string
- Railsのフォーム入力画面で発生したエラー
- 入力値にenumを使用
- BootStrapのクラスを適用する時
{:class =>"form-control
}は記号でも文字列でもないよ`(by Google 翻訳)- そもそも
enum
を使うときはf.select
みたい(いろいろ参考)- o f.select
- x f.collection_select
- ここが分かりやすかった - Railsでenumを使ったselectタグ | 酒と涙とRubyとRailsと
<!-- bad(error) --> <%= f.collection_select(:difficulty, Cuisine.difficulties.keys.map{|k| [ k , k]}, {prompt: "選択してください", required: true}, {class: 'form-control'}) %> <!-- good(no error) --> <%= f.select(:difficulty, Cuisine.difficulties.keys.map{|k| [ k , k]}, {prompt: "選択してください", required: true}, {class: 'form-control'}) %>
ActionView::Template::Error - nil is not a valid asset source
- Viewの
image_tag
ヘルパーによるエラー - Rails5からnilチェックが必要(unless文を追加する)
【Rails5】nil is not a valid asset source が出た時の対処法 | KANOE STUDIO
<!-- Rails4 までok --> <%= image_tag(user.image , class: "responsive-img") %> <!-- Rails5 からは下記の書き方で --> <%= image_tag(user.image , class: "responsive-img") unless user.image.blank?%>
ActionView::Template::Error - undefined method `stringify_keys'
- Viewのヘルパーで発生したエラー
- link_to ヘルパーをブロックで使用するときに 文字列を削除し忘れた時に発生
- link_to 直後の文字列を削除してあげればエラーが発生しなくなりました
<!--NG--> <%= link_to '詳細', xxx_path(xxx), do %> aaa <% end %> <!--OK--> <%= link_to xxx_path(xxx), do %> aaa <% end %>
ActionView::Template::Error at /xxx - undefined method `yyy' for nil:NilClass
- Viewの association関連のエラー
- 書き方が合っていても怒られる場合がある(今回がそれ)
- 原因はデータの整合性がとれていないこと(今回はseedデータの作成データにズレが生じた)
- foodcategory:1 - N:foodstuff
- foodstuffsレコードのfoodcategory_idに存在しない値が入っていた
- 該当するレコードを修正 or 削除すればエラーが解消される
- seedデータは開発中のデータに問題がなければそれを取り込んだ方が良いかも
ActiveRecord::NoEnvironmentInSchemaError:
- DBをリセット(rails db:migrate:reset)しようとした時に発生(下記がエラー表示)
- Rails5以降のDB保護機能で、誤ってproduction environmentのDBを破壊してしまうのを防ぐ為の仕組みらしい - Rails 5に入ったDB破壊系taskの防止処理について | 日々雑記
- 表示の通り
bin/rails db:environment:set RAILS_ENV=development
を実行すれば解決できた - guguru
rails aborted! ActiveRecord::NoEnvironmentInSchemaError: Environment data not found in the schema. To resolve this issue, run: bin/rails db:environment:set RAILS_ENV=development /usr/local/bundle/gems/activerecord-5.1.7/lib/active_record/migration.rb:1275:in `last_stored_environment' /usr/local/bundle/gems/activerecord-5.1.7/lib/active_record/tasks/database_tasks.rb:56:in `check_protected_environments!' /usr/local/bundle/gems/activerecord-5.1.7/lib/active_record/railties/databases.rake:11:in `block (2 levels) in <top (required)>' /usr/local/bundle/gems/railties-5.1.7/lib/rails/commands/rake/rake_command.rb:21:in `block in perform' /usr/local/bundle/gems/railties-5.1.7/lib/rails/commands/rake/rake_command.rb:18:in `perform' /usr/local/bundle/gems/railties-5.1.7/lib/rails/command.rb:46:in `invoke' /usr/local/bundle/gems/railties-5.1.7/lib/rails/commands.rb:16:in `<top (required)>' bin/rails:4:in `require' bin/rails:4:in `<main>' Tasks: TOP => db:migrate:reset => db:drop => db:check_protected_environments
Association named 'xxx' was not found on yyy; perhaps you misspelled it?
- viewファイルを表示するときに発生
yyyモデルにはxxxていうモデルと関連付けがないよ: もしかしたらつづりミスかも
という内容- model同士の関連付け(has_one, has_many, belongs_toなど)が記述してあるか
- 書いてあったらスペルミスがないかチェックすればだいたい大丈夫
Errno::EIO: Input/output error
Errno::EXDEV: Invalid cross-device link
も同時に発生しました。- gem
seed_fu
で初期データ投入時に発生しました。 - 原因不明? 別タブでrailsを動作させていたserverを停止させて再度
rails db:seed_fu
を実行したらエラーは発生しませんでしたw - Rails3 でサーバ起動させると「Errno::EIO: Input/output error 〜 」とかなる場合のメモ - 牌語備忘録 -pygo
Routing Error(No route matches [GET] "/sign_out")
- deviseのログアウトで発生(他のdestroyアクション全てでも発生した)
- 原因は不明(いきなり上記のエラーが発生)
- ここを参考にしました - No route matches [DELETE] "/users/sign_out" エラー(Rails) - Qiita
- 私の場合は下記の修正のみでOKでした
- ※修正したらサーバーを再起動させる
# before config.sign_out_via = :delete # after config.sign_out_via = :get
Routing Error(No route matches [GET] "/xxx")
- destroyアクションでエラー発生
- 上記のエラーが発生してから起きるように
link_to
をbutton_to
に変更すれば解決できた - No route matches [GET]の原因と解決策 - Qiita
# before <%= link_to '削除', xxx_path(fc.id), method: :delete %> # after <%= button_to '削除', xxx_path(fc.id), method: :delete %>
SystemStackError at xxx stack level too deep
- 部分テンプレートが同じ部分テンプレートファイルを呼び出す無限ループ文を書いていた(コピペが原因)
- e.g
aaas/new.html.erb
内の<%= render 'aaa_form', {form: form ,aaa: @aaa} %>
のrender元のファイル_aaa_form.html.erb
の中で<%= render 'aaa_form', {form: form ,aaa: @aaa} %>
を書いていた
- e.g
- 私の場合はこんな高度なお話ではありません
Docker
Named volume "yarn-cache:/usr/local/share/.cache/yarn/v6:rw" is used in service "web" but no declaration was found in the volumes section
- こんな感じでggりました - docker yarn Named volume is used in service web but no declaration was found in the volumes section. - Google 検索
- この記事と同じ原因でした(
volumes
の書き方に問題があったみたい) - ERROR: Named volume "data:/var/lib/mysql:rw" is used in service "xxxx" but no declaration was found in the volumes section. - grmn's blog - yarnだけでなくbundler、db関係でも使える
# bad version: '3.7' services: db: : volumes: - db-data:/var/lib/mysql web: : volumes: - .:/webapp - yarn-cache:/usr/local/share/.cache/yarn/v6 : volumes: db-data: # good version: '3.7' services: db: : volumes: - db-data:/var/lib/mysql web: : volumes: - .:/webapp - yarn-cache:/usr/local/share/.cache/yarn/v6 : volumes: db-data: yarn-cache:
volumes:
について怪しい場合はこちらがいいかも - Dockerにおけるボリュームのマウント - logicoffee プログラミング勉強日記
エラーではないけれど
destroyアクション(DELETEメソッド)を要求したのにGETメソッドが送信される。
- Rails5.1で発生した問題
- 下記の設定をすればDELETEメソッドとして送信されるようになりました
// app/javascript/packs/application.js import Rails from 'rails-ujs/lib/assets/compiled/rails-ujs' : : Rails.start()