Ruby on rails 无法使用Rails创建MongoDB文档
我在使用MongoDB和Rails时遇到了很多问题。我正在尝试创建用户提要,但当我试图保存文档时,Rails一直在侮辱我。我只是不知道发生了什么,而且据我所知,这些文档似乎没有什么帮助,我已经按书做了所有的事情 以下是我所做的关系:Ruby on rails 无法使用Rails创建MongoDB文档,ruby-on-rails,mongodb,mongoid,Ruby On Rails,Mongodb,Mongoid,我在使用MongoDB和Rails时遇到了很多问题。我正在尝试创建用户提要,但当我试图保存文档时,Rails一直在侮辱我。我只是不知道发生了什么,而且据我所知,这些文档似乎没有什么帮助,我已经按书做了所有的事情 以下是我所做的关系: class Feed include Mongoid::Document field :name, :type => String field :description, :type => String
class Feed
include Mongoid::Document
field :name, :type => String
field :description, :type => String
has_many :feedposts
embedded_in :user
end
class Feedpost
include Mongoid::Document
field :title => String
field :text => String
field :resource => String
field :time => Time, :default => -> { Time.now }
belongs_to :feed
embeds_many :comments
end
class Comment
include Mongoid::Document
has_one :user
field :text => String
field :time => Time, :default => -> { Time.now }
embedded_in :feedpost
end
当试图保存一个Feed时,没有提示Feedpost或评论,它只是告诉我新的方法?未为类定义。
我从Rails得到了这个回溯:
activesupport (3.1.0.rc5) lib/active_support/whiny_nil.rb:48:in `method_missing'
mongoid (2.2.0) lib/mongoid/persistence/operations/embedded/insert.rb:31:in `block in persist'
mongoid (2.2.0) lib/mongoid/persistence/insertion.rb:26:in `block (3 levels) in prepare'
activesupport (3.1.0.rc5) lib/active_support/callbacks.rb:390:in `_run_create_callbacks'
activesupport (3.1.0.rc5) lib/active_support/callbacks.rb:81:in `run_callbacks'
mongoid (2.2.0) lib/mongoid/persistence/insertion.rb:25:in `block (2 levels) in prepare'
activesupport (3.1.0.rc5) lib/active_support/callbacks.rb:390:in `_run_save_callbacks'
activesupport (3.1.0.rc5) lib/active_support/callbacks.rb:81:in `run_callbacks'
mongoid (2.2.0) lib/mongoid/persistence/insertion.rb:24:in `block in prepare'
mongoid (2.2.0) lib/mongoid/persistence/insertion.rb:22:in `tap'
mongoid (2.2.0) lib/mongoid/persistence/insertion.rb:22:in `prepare'
mongoid (2.2.0) lib/mongoid/persistence/operations/embedded/insert.rb:30:in `persist'
mongoid (2.2.0) lib/mongoid/persistence.rb:44:in `insert'
mongoid (2.2.0) lib/mongoid/persistence.rb:149:in `upsert'
编辑:
这是控制器:
class FeedsController < ApplicationController
def index
@user = CurrentUser()
@feeds = @user.feeds
end
def create
@feed = Feed.new params[:feed]
success = @feed.save # This is the line that trigger the error
render json: { :success => success }
end
end
我到底怎么了?有人有想法吗?您可能需要发布更多关于mongodb设置的信息。你能在里面存储任何东西吗?正如mkro提到的,我们需要看到你的控制器代码,只是为了在这里插话。我认为这不是完整的回溯,因为它是从activesupport中的代码开始的。你能发布整个跟踪吗?是哪一行触发了错误?回溯中缺少的那一行是异常NoMethodError的本质,它是nil:NilClass的未定义方法“new”,由注释为的控制器行触发。谢谢你的帮助,我想我已经达到了要求:!请将您发送给控制器的参数发布。
development:
host: localhost
database: noosphere_development