Rails, mongoid 抛出 TypeError no implicit conversion of nil into String

Rails, mongoid throws TypeError no implicit conversion of nil into String

1) 我已经安装了 MongoDB (Windows 7 64bit)。创建了 "data/db" 路径并通过 cmd 启动了 mongod

2) 我使用 RubiMine 7 创建了 Rails 项目。我为我的 Gemfile:

添加了两行
gem 'mongoid'
gem 'bson_ext'

然后我进行了捆绑安装。

3) 我有 运行 rails 生成器用于 mongoid:config,这添加到我 mongoid.yml 文件在我的配置文件夹中。

4) 创建了带有字符串和文本属性的脚手架。这游戏我就是那种模式:

class Article
    include Mongoid::Document
    field :name, type: String
    field :content, type: String
end

然后我启动了我的服务器。当我打开我的 localhost:3000/articles url 它给我 TypeError no implicit conversion of nil into String 错误。

我不明白我做错了什么?我使用的是 ruby、rails、mongodb 和 mongoid 的最新版本。

我的日志:

Started GET "/articles" for 127.0.0.1 at 2015-02-28 14:30:29 +0600
Processing by ArticlesController#index as HTML
Rendered articles/index.html.erb within layouts/application (3.0ms)
Completed 500 Internal Server Error in 16ms

ActionView::Template::Error (no implicit conversion of nil into String):
12:   </thead>
13: 
14:   <tbody>
15:     <% @articles.each do |article| %>
16:       <tr>
17:         <td><%= article.name %></td>
18:         <td><%= article.content %></td>
app/views/articles/index.html.erb:15:in
Rendered D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/web-console-     2.0.0/lib/action_dispatch/templates/rescues/_source.erb (3.0ms)
Rendered D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_trace.html.erb (5.0ms)
Rendered D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_request_and_response.html.erb (1.0ms)
Rendered D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_web_console.html.erb (1.0ms)
Rendered D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/template_error.html.erb within   rescues/layout (28.0ms)

更新:

我启动了 rails 控制台并开始自己输入命令:

a = Article.new
a.title = "AAAA"
a.content = "BBBB"
a.save

这给我带来了那种错误:

TypeError: no implicit conversion of nil into String
from D:/Ruby21-x64/lib/ruby/2.1.0/resolv.rb:190:in `open'
from D:/Ruby21-x64/lib/ruby/2.1.0/resolv.rb:190:in `block in  lazy_initialize'
from D:/Ruby21-x64/lib/ruby/2.1.0/resolv.rb:186:in `synchronize'
from D:/Ruby21-x64/lib/ruby/2.1.0/resolv.rb:186:in `lazy_initialize'
from D:/Ruby21-x64/lib/ruby/2.1.0/resolv.rb:237:in `each_address'
from D:/Ruby21-x64/lib/ruby/2.1.0/resolv.rb:115:in `block in each_address'
from D:/Ruby21-x64/lib/ruby/2.1.0/resolv.rb:114:in `each'
from D:/Ruby21-x64/lib/ruby/2.1.0/resolv.rb:114:in `each_address'
from D:/Ruby21-x64/lib/ruby/2.1.0/resolv.rb:57:in `each_address'
from D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/moped-2.0.4/lib/moped/address.rb:51:in `block in resolve'
from D:/Ruby21-x64/lib/ruby/2.1.0/timeout.rb:91:in `block in timeout'
from D:/Ruby21-x64/lib/ruby/2.1.0/timeout.rb:35:in `block in catch'
from D:/Ruby21-x64/lib/ruby/2.1.0/timeout.rb:35:in `catch'
from D:/Ruby21-x64/lib/ruby/2.1.0/timeout.rb:35:in `catch'
from D:/Ruby21-x64/lib/ruby/2.1.0/timeout.rb:106:in `timeout'
from D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/moped-2.0.4/lib/moped/address.rb:50:in `resolve'
... 25 levels...
from D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_save_callbacks'
from D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:81:in `run_callbacks'
from D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/mongoid-4.0.2/lib/mongoid/interceptable.rb:138:in `run_callbacks'
from D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/mongoid-4.0.2/lib/mongoid/persistable/creatable.rb:116:in `prepare_insert'
from D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/mongoid-4.0.2/lib/mongoid/persistable/creatable.rb:23:in `insert'
from D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/mongoid-4.0.2/lib/mongoid/persistable/savable.rb:23:in `save'
from (irb):15
from D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from D:/Phantasy star/Mobi/Daniars_Project/Svadba/mongo_id_test/bin/rails:4:in `require'
from D:/Phantasy star/Mobi/Daniars_Project/Svadba/mongo_id_test/bin/rails:4:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'

即使仅使用 rails 控制台也会抛出 SAME ERROR

你最新的回溯显示你没有指定 MongoDB 的主机地址。阅读 this 部分关于配置 Mongoid gem。

更新

可能你的 mongoid.yml 格式不正确。

更新 2

运行 在 irb 控制台中执行以下命令并提供输出:

$  ~  irb
2.1.0 :001 > require 'resolv'
 => true
2.1.0 :002 > Resolv::Hosts::DefaultFileName
 => "/etc/hosts"

您似乎在 windows 中没有 hosts 文件或文件格式不正确。如果你有它也提供它的内容。