尝试Heroku运行rake db:migs with cors时突然出现“ config”错误?

时间:2019-05-13 14:45:09

标签: ruby-on-rails heroku cors

我正在阅读有关将React / Rails应用程序部署到Heroku的教程。我没有更改config/initializers/cors.rb中的任何内容,但是Rails突然从该文件中抛出了NO Method error。我尝试bundle install。我也将cors文件更改为通配符,只是为了确定(这是我的Heroku应用程序的特定地址),但仍然出现以下错误:


Running rake db:migrate on ⬢ faunagram-api... up, run.4653 (
Free)
rake aborted!
NameError: undefined local variable or method `config' for main:Object
/app/config/initializers/cors.rb:10:in `<main>'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:285:in `block in load'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:285:in `load'
/app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.2/lib/rails/engine.rb:657:in `block in load_config_initializer'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.2/lib/active_support/notifications.rb:170:in `instrument'
/app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.2/lib/rails/engine.rb:656:in `load_config_initializer'
/app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.2/lib/rails/engine.rb:614:in `block (2 levels) in <class:Engine>'
/app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.2/lib/rails/engine.rb:613:in `each'
/app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.2/lib/rails/engine.rb:613:in `block in <class:Engine>'
/app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.2/lib/rails/initializable.rb:32:in `instance_exec'
/app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.2/lib/rails/initializable.rb:32:in `run'
/app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.2/lib/rails/initializable.rb:61:in `block in run_initializers'
/app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.2/lib/rails/initializable.rb:50:in `each'
/app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.2/lib/rails/initializable.rb:50:in `tsort_each_child'
/app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.2/lib/rails/initializable.rb:60:in `run_initializers'
/app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.2/lib/rails/application.rb:361:in `initialize!'
/app/config/environment.rb:5:in `<main>'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:83:in `register'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `block in require'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:257:in `load_dependency'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.2/lib/active_support/dependencies.rb:291:in `require'
/app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.2/lib/rails/application.rb:337:in `require_environment!'
/app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.2/lib/rails/application.rb:520:in `block in run_tasks_blocks'
/app/vendor/bundle/ruby/2.6.0/gems/rake-12.3.2/exe/rake:27:in `<top (required)>'
/app/bin/bundle:3:in `load'
/app/bin/bundle:3:in `<main>'
Tasks: TOP => db:migrate => db:load_config => environment
(See full trace by running task with --trace)

我的Cors文件:

# Be sure to restart your server when you modify this file.

# Avoid CORS issues when API is called from the frontend app.
# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests.

# Read more: https://github.com/cyu/rack-cors

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'

    resource '*',
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

# `
# Rails.application.config.middleware.insert_before 0, Rack::Cors do
#  allow do

#    if Rails.env.development?
#      origins ‘*’
#      else
#        origins [‘https://yourapp.herokuapp.com','http://yourapp.herokuapp.com']
#    end
#    resource ‘*’,
#      headers: :any,
#      methods: [:get, :post, :put, :patch, :delete, :options, :head]
#  end
# end`

我不确定发生了什么变化,但是我无法heroku run rake db:migrate

1 个答案:

答案 0 :(得分:0)

这与我在rails 5.2上一起使用,您没有告诉我们您的rails版本是什么

    config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :options]
      end
    end