新手问题 Windows 下面 Ruby 错误 Could not load 'active_record/connection_adapters/sqlite3_adapter'.

stanwang · 2014年09月23日 · 最后由 ywjno 回复于 2014年09月29日 · 3522 次阅读
  • Ruby 环境:ruby 2.1.3p242 (2014-09-19 revision 47630) [x64-mingw32]
  • Rails 环境:Rails 4.1.6
  • Widows 环境:win7 64bit

sqlite3-1.3.9-x64-mingw32.gemspec 文件中已修改为 s.require_paths= ["lib/sqlite3_native"]

相关的 dll 文件也放入 Ruby 的 Bin 文件之中,而且 Gemfile 文件中的 sqlite3 和 Gemfile.lock 中的版本一致,但是访问网页会抛出错误如下:

Could not load 'active_record/connection_adapters/sqlite3_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile.

异常链如下:

activesupport (4.1.6) lib/active_support/dependencies.rb:247:in `require'
activesupport (4.1.6) lib/active_support/dependencies.rb:247:in `block in require'
activesupport (4.1.6) lib/active_support/dependencies.rb:232:in `load_dependency'
activesupport (4.1.6) lib/active_support/dependencies.rb:247:in `require'
activerecord (4.1.6) lib/active_record/connection_adapters/sqlite3_adapter.rb:6:in `<top (required)>'
activesupport (4.1.6) lib/active_support/dependencies.rb:247:in `require'
activesupport (4.1.6) lib/active_support/dependencies.rb:247:in `block in require'
activesupport (4.1.6) lib/active_support/dependencies.rb:232:in `load_dependency'
activesupport (4.1.6) lib/active_support/dependencies.rb:247:in `require'
activerecord (4.1.6) lib/active_record/connection_adapters/connection_specification.rb:188:in `spec'
activerecord (4.1.6) lib/active_record/connection_handling.rb:50:in `establish_connection'
activerecord (4.1.6) lib/active_record/railtie.rb:129:in `block (2 levels) in <class:Railtie>'
activesupport (4.1.6) lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
activesupport (4.1.6) lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
activesupport (4.1.6) lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
activesupport (4.1.6) lib/active_support/lazy_load_hooks.rb:44:in `each'
activesupport (4.1.6) lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
activerecord (4.1.6) lib/active_record/base.rb:326:in `<module:ActiveRecord>'
activerecord (4.1.6) lib/active_record/base.rb:23:in `<top (required)>'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:628:in `rescue in call'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:619:in `call'
activerecord (4.1.6) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.6) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.6) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.6) lib/rails/engine.rb:514:in `call'
railties (4.1.6) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
E:/Tool/Ruby21-x64/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
E:/Tool/Ruby21-x64/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
E:/Tool/Ruby21-x64/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

求解惑~~

Ruby 不熟悉的话,还是不要在 Windows 下面搞,你这些事情都是多余的。

新来的第一篇就献给你了兄弟: 这个问题我也折腾了几个小时,解决这个问题需要重新编译和安装 gem sqlite3, 要想编译安装本地 gem,需要 Ruby 的 DevKit,请先下载安装: Win32 版本:http://cdn.rubyinstaller.org/archives/devkits/DevKit-mingw64-32-4.7.2-20130224-1151-sfx.exe Win64 版本:http://cdn.rubyinstaller.org/archives/devkits/DevKit-mingw64-64-4.7.2-20130224-1432-sfx.exe

按照文章中说明:https://github.com/oneclick/rubyinstaller/wiki/Development-KitDevKit,安装 1) 执行 DevKit 初始化

>ruby dk.rb init

它会生成一个空的 config.yml 文件,把你的 Ruby 安装路径填进去,如:- D:/Ruby21-x64 2) 执行安装

>ruby dk.rb install

这样就可以了,测试一下:

>gem install json --platform=ruby
>ruby -rubygems -e "require 'json'; puts JSON.load('[42]').inspect"

如果输出 [42] 就 OK 了

接下来重装 gem sqlite3. 先下载所需文件: Win32 版本 DLL:http://www.sqlite.org/snapshot/sqlite-dll-win32-x86-201409200035.zip Win64 版本 DLL:http://www.sqlite.org/snapshot/sqlite-dll-win64-x64-201409200035.zip sqlite 源代码:http://www.sqlite.org/2014/sqlite-autoconf-3080600.tar.gz

解压文件之后,先卸载之前安装的 sqlite3:

>gem uninstall sqlite3

如果有多个的话都给它卸掉,然后编译安装新的 gem:

>gem install sqlite3 --platform=ruby -- --with-sqlite3-include=D:/sqlite3/src  --with-sqlite3-lib=D:/sqlite3/bin

其中--with-sqlite3-include 对应 sqlite 源码(sqlite3.h 所在)文件夹,--with-sqlite3-lib 对应 dll 所在文件夹。安装好 gem sqlite3 之后就万事大吉了。

建议装回 ruby2.0.0 那个版本,没有任何问题。还有 win64 版本的 ruby 编译 mysql 有问题。32 位没问题。

写 ruby 别在 win 下搞了

其实 win 下的 ruby 还是装 version 1.9.3 的好,非要装 2.0.0 的话推荐装 32 位的,64 位上次看到说要在注册表添加一个值我一下给忘了。

不能因为某些 gem 无法在 win 下用而断言 ruby 在 win 里面不行。这是写 gem 的人造成的,而不是 ruby 造成的

stanwang 关闭了讨论。 09月23日 23:12
需要 登录 后方可回复, 如果你还没有账号请 注册新账号