前提・実現したいこと
RailsAPIでオリジンからのアクセスを許可し、フロントエンドからAPIを叩きたいです。
その際、
gem rack-cors
の追加
bundle
の実行
cors.rb
の編集(以下参照)
を行いました。
ruby
1#cors.rb 2# Be sure to restart your server when you modify this file. 3 4# Avoid CORS issues when API is called from the frontend app. 5# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests. 6 7# Read more: https://github.com/cyu/rack-cors 8 9Rails.application.config.middleware.insert_before 0, Rack::Cors do 10 allow do 11 origins '*' 12 resource '*', 13 headers: :any, 14 methods: [:get, :post, :put, :patch, :delete, :options, :head] 15 end 16end 17
発生している問題・エラーメッセージ
NameErrorが発生してしまいます
api_1 | /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/active_support.rb:80:in `block in load_missing_constant': uninitialized constant Rack::Cors (NameError) api_1 | from /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/active_support.rb:9:in `without_bootsnap_cache' api_1 | from /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/active_support.rb:80:in `rescue in load_missing_constant' api_1 | from /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/active_support.rb:59:in `load_missing_constant' api_1 | from /app_name/api/config/application.rb:25:in `<class:Application>' api_1 | from /app_name/api/config/application.rb:21:in `<module:Api>' api_1 | from /app_name/api/config/application.rb:20:in `<top (required)>' api_1 | from /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' api_1 | from /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' api_1 | from /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' api_1 | from /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' api_1 | from /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' api_1 | from /usr/local/bundle/gems/railties-5.2.4.3/lib/rails/commands/server/server_command.rb:145:in `block in perform' api_1 | from /usr/local/bundle/gems/railties-5.2.4.3/lib/rails/commands/server/server_command.rb:142:in `tap' api_1 | from /usr/local/bundle/gems/railties-5.2.4.3/lib/rails/commands/server/server_command.rb:142:in `perform' api_1 | from /usr/local/bundle/gems/thor-1.0.1/lib/thor/command.rb:27:in `run' api_1 | from /usr/local/bundle/gems/thor-1.0.1/lib/thor/invocation.rb:127:in `invoke_command' api_1 | from /usr/local/bundle/gems/thor-1.0.1/lib/thor.rb:392:in `dispatch' api_1 | from /usr/local/bundle/gems/railties-5.2.4.3/lib/rails/command/base.rb:69:in `perform' api_1 | from /usr/local/bundle/gems/railties-5.2.4.3/lib/rails/command.rb:46:in `invoke' api_1 | from /usr/local/bundle/gems/railties-5.2.4.3/lib/rails/commands.rb:18:in `<top (required)>' api_1 | from /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' api_1 | from /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' api_1 | from /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' api_1 | from /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' api_1 | from /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' api_1 | from bin/rails:4:in `<main>' prologue_api_1 exited with code 1
補足情報(FW/ツールのバージョンなど)
ruby-2.5.3
rails-5.2.4
あなたの回答
tips
プレビュー