toshizou-Rails

Railsアプリ作成するときに遭遇したエラーたち(随時追加、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

{:class=>"form-control"} is not a symbol nor a string

<!-- 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

<!-- 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:

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

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")

# 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} %>を書いていた
  • 私の場合はこんな高度なお話ではありません

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

# 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:

エラーではないけれど

destroyアクション(DELETEメソッド)を要求したのにGETメソッドが送信される。

// app/javascript/packs/application.js
import Rails from 'rails-ujs/lib/assets/compiled/rails-ujs'
:
:
Rails.start()