【问题标题】:Heroku Deploy: URI::InvalidURIError: bad URI(is not URI?): ://user:pass@127.0.0.1/dbnameHeroku Deploy: URI::InvalidURIError: bad URI(is not URI?): ://user:pass@127.0.0.1/dbname
【发布时间】:2020-02-02 14:10:28
【问题描述】:

我有一个带有 Vue 前端和 Rails 后端的项目。它只是后端的一个非常简单的 API,没有数据库或任何东西。它在本地运行良好,但现在我想在 Heroku 上部署它。

但是,当我运行它时,我收到以下错误。

-----> Detecting rake tasks
 !
 !     Could not detect rake tasks
 !     ensure you can run `$ bundle exec rake -P` against your app
 !     and using the production group of your Gemfile.
 !     rake aborted!
 !     URI::InvalidURIError: bad URI(is not URI?): ://user:pass@127.0.0.1/dbname
  ...
  ...
/activerecord-6.0.2.1/lib/active_record/railties/databases.rake

根据各种 SO 帖子/Heroku 文档,我已经尝试过:

  • bundle exec rake -P RAILS_ENV=production - 一切正常
  • 在 Gemfile 中添加 rake 依赖项
  • 移除 Gemfile 中的 sqlite 依赖项
  • 从 Gemfile.lock 中删除 BUNDLED WITH

但还是同样的错误。

我猜这与我的数据库配置有关,但我的项目中没有任何数据库,所以这似乎是一项不必要的任务。我尝试从我的 Gemfile 中注释掉 railties,但它仍然作为其他 gem 的依赖项存在。当我在进行此更改后进行部署时,它仍然会执行相同的任务并失败。

链接到repo branch

【问题讨论】:

    标签: ruby-on-rails heroku railtie


    【解决方案1】:

    require 'rails/all' 需要所有 Railties,包括 ActiveRecord,您需要明确要求您要使用的 railties:

    require File.expand_path('../boot', __FILE__)
    
    # Require the gems listed in Gemfile, including any gems
    # you've limited to :test, :development, or :production.
    Bundler.require(*Rails.groups)
    
    require "rails"
    # Pick the frameworks you want:
    require "active_model/railtie"
    require "active_job/railtie"
    # require "active_record/railtie"
    # require "active_storage/engine"
    require "action_controller/railtie"
    require "action_mailer/railtie"
    require "action_mailbox/engine"
    # require "action_text/engine"
    require "action_view/railtie"
    require "action_cable/engine"
    require "sprockets/railtie"
    
    module Draw
      class Application < Rails::Application
        # You don't need this nonsense since you don't even have config/application.yml
        #  ENV.update YAML.load_file('config/application.yml')[Rails.env] rescue {}
        # Settings in config/environments/* take precedence over those specified here.
        # Application configuration should go into files in config/initializers
        # -- all .rb files in that directory are automatically loaded.
    
        # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
        # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
        # config.time_zone = 'Central Time (US & Canada)'
    
        # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
        # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
        # config.i18n.default_locale = :de
    
        # Do not swallow errors in after_commit/after_rollback callbacks.
        # config.active_record.raise_in_transactional_callbacks = true
      end
    end
    

    如果你不想使用 ActiveRecord,你可以去掉 /db/config/database.yml

    您也不需要在 Gemfile 中添加 gem 'rake',因为无论如何 rails 都依赖它。

    【讨论】:

    • 我不确定我要使用什么轨道,如果有的话......我什至不确定轨道是什么。
    • 我也刚刚注意到,由于这是作为 API 的 Rails 实现,我的应用程序控制器应该扩展 ActionController::API 而不是 ::Base。我不知道为什么它是我在创建应用程序时指定 --api 的 Base
    • 即使只需要那些特定的轨道,它仍然会发生
    • rails new app_name --skip-active-record --api
    • 您必须在配置文件中重新添加railtie、数据库配置和活动记录选项。在这种情况下,您可能只想运行 rails new app_name --api --database=postgresql 并从一开始就进行设置。您可能想在迁移代码之前尝试部署,这样您就不会只是重新引入任何潜在的错误。
    【解决方案2】:

    需要从项目中完全删除对 ActiveRecord 的使用。作为Max commented,在一个新的应用程序中,这可以通过rails new app_name --skip-active-record --api来完成,对于现有项目,请参见this explanation

    【讨论】:

      猜你喜欢
      • 2017-12-18
      • 1970-01-01
      • 2020-06-11
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 2018-06-12
      • 2012-02-23
      • 2011-07-19
      相关资源
      最近更新 更多