【问题标题】:Why is my rails server issuing deprecation warning and not connection to sqlite3?为什么我的 Rails 服务器发出弃用警告而不是连接到 sqlite3?
【发布时间】:2013-11-24 14:37:46
【问题描述】:

我正在按照本教程开始使用 rails: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec-the_first_application 这看起来很不错。

一开始,作者谈到了版本对宝石和软件的重要性,所以我尽量保持使用完全相同的版本。

我按照教程进行,一切运行良好,安装正常(来自他建议的来源:http://railsinstaller.org/en)我下载了 ruby​​ 1.9。

安装后,我使用 rails new first_app 创建了我的应用程序,将 Gemfile 更改为这个:

source 'https://rubygems.org'
ruby '1.9.3' #In the tutorial is 2.0.0, but changed to match my ruby version, 
             #as specified in the tutorial
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.0.1'

group :development do
  gem 'sqlite3', '1.3.8'
end

gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

当我运行 rails server 命令时,我收到以下错误:

    DEPRECATION WARNING: config.whiny_nils option is deprecated and no longer works.
 (called from block in <top (required)> at D:/rails/first_app/config/environment
s/development.rb:10)
config.eager_load is set to nil. Please update your config/environments/*.rb fil
es accordingly:

  * development - set it to false
  * test - set it to false (unless you use a tool that preloads your test enviro
nment)
  * production - set it to true

但是打开 localhost:3000 可以正常工作。点击“About your application's environment”链接,会产生错误

ActiveRecord::ConnectionNotEstablished

Rails.root: D:/rails/first_app

我检查了一下,我的 database.yml 正在使用 sqlite3。

当我在我的应用文件夹中运行 rake db:create 时,我得到了

rake 中止! 为数据库适配器指定了“postgresql”,但 gem 不是 加载。将gem 'pg' 添加到您的 Gemfile。

我认为这三个问题可能是相关的,问题的根源是启动 rails 服务器时的错误消息。 我能做些什么来修复它,它会是 ruby​​ 版本 1.9.3 而不是 2.0.0?

谢谢!

编辑: On this link,我在 whiny_nils 弃用问题上找到了解决方案

Rails 4 removed the whiny_nils feature. Read more about it in the ActiveRecord chapter.

To solve the deprecation warning, simply remove any lines that set config.whiny_nils. Rails 3 added the configuration by default in config/environments/development.rb and config/environments/test.rb by default.

不知道为什么创建一个应用程序并以相同的版本启动它会导致这个问题,但没关系。 1号固定:)

Edit2:在同一个链接中,我通过在我的配置文件中创建此配置并设置一个值来修复 config.eager_load 问题。

活动记录问题仍然存在。

编辑 3: 这是我的 database.yml 文件

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

【问题讨论】:

  • 如果你要使用 'postresql' 数据库(在 /config/database.yml 中),那么你必须在 Gemfile 中添加 postresql gem (gem 'pg')
  • 不,我想使用 sqlite3 数据库。我不知道为什么 rake 引用 postgres :(
  • 检查您的 /config/database.yml 文件是否有任何数据库的适配器设置为 postgresql
  • 更新问题,但不,它没有。我确实有其他使用 postgres 的应用程序,但这个应该使用 sqlite。 rake 是否有可能从其他应用程序获取适配器信息?
  • Rails 默认会在 /db/development.sqlite3 中创建一个 sqlite 数据库。所以只需创建你的模型并运行rake db:migrate。

标签: ruby-on-rails sqlite3-ruby


【解决方案1】:

已解决:我的系统环境变量中有一个指向 postgres 数据库的 database_url。我很久以前在学习heroku教程时已经这样做了。我删除了它,现在它可以正常工作了。

【讨论】:

    【解决方案2】:

    我建议一个我认为对您更有效的解决方案:

    从除 rails 之外的所有 gem 中删除版本号

    它将执行 rails 4 和 ruby​​ 2.0 并且对您而言 ruby​​ 2.0 可能会工作。 如有必要,将 ruby​​ 版本设置为 1.9.3

    现在和将来这可能对您更有效。
    最好避免其他 gem 的所有这些特定版本号,以避免出现....您遇到的版本问题。您希望在实际应用程序、rails 代码、ruby 代码等上花费更少的时间。大多数 gem 可以自己找出正确的版本和依赖项。

    尝试以这种方式快速创建另一个应用程序(确保在再次发出该 rails new 命令之前先“cd ..”退出此应用程序)。您还会发现,与其他一些较旧的框架相比,开发新应用程序的频率惊人。

    【讨论】:

    • 嗨@Michael 感谢您的回答。您能否详细说明我删除版本会更好吗?我遵循的教程正好相反:设置版本是最好的,这样你就有一个定义的“框架”,并避免新版本的包破坏你的环境。
    • 虽然我可以理解教程的建议以准确指定所有内容,但内容变化如此之快以至于这种硬编码只会导致问题,尤其是在所有这些辅助宝石中。好消息是 Rails 4.0 无论如何现在都是“最新的”。所以我的观点是,最多可以指定 rails 和 ruby​​ 版本,但是停在那里,否则您将开始遇到有关所有辅助 gem 的版本号的问题,您将浪费时间解决而不是学习 rails 和编写应用程序。
    • 是的,这是有道理的。在创建“真正的应用程序”时,我将只定义这两个版本。但是对于教程,我会尽量保持原样使用版本,所以我和作者有一个相同的环境:) 谢谢你的建议!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多