Rails,mongoid throws TypeError没有将nil隐式转换为String

时间:2015-02-28 09:01:12

标签: ruby-on-rails ruby mongodb ruby-on-rails-4 mongoid

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

2)我使用RubiMine 7创建了Rails项目。我为 Gemfile 添加了两行:

gem 'mongoid'
gem 'bson_ext'

然后我进行了捆绑安装。

3)我已经为 mongoid:config 运行了rails生成器,这在我的配置文件夹中添加了 mongoid.yml 文件。

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

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

然后我启动了我的服务器。当我打开我的localhost:3000 / articles url时,它给了我 TypeError没有将nil隐式转换为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 console并开始自己输入命令:

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

1 个答案:

答案 0 :(得分:1)

您的最新回溯表明您没有指定MongoDB的主机地址。阅读有关配置Mongoid gem的this部分。

<强>更新

也许你的mongoid.yml格式不正确。

更新2

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

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

好像你在Windows中没有hosts文件或格式不正确。如果你有,也提供内容。