使用 MongoMapper 创建 Rails 引擎
我正在尝试使用 MongoMapper 创建 Rails 3 引擎。我正在经历一个痛苦的世界,让它继续下去。这是我的模型:
module GoodComments
class Comment
include MongoMapper::Document
key :comment, String
end
end
超级简单,我知道!我的 config/routes.rb:
GoodComments::Engine.routes.draw do
resources :comments
end
我创建了 config/application.rb:
require File.expand_path('../boot', __FILE__)
module GoodComments
class Application < Rails::Application
config.generators do |g|
g.orm :mongo_mapper # :active_record
g.template_engine :erb # :haml
g.test_framework :rspec, :fixture => true, :views => false
g.fixture_replacement :factory_girl, :dir => "spec/factories"
end
end
end
我运行了 railsgeneratescaffold_controllerComment -o mongo_mapper
并且我的控制器是生成的。当我运行服务器并转到 http://localhost:3000/good_comments/comments
时,出现错误:
LoadError in GoodComments::CommentsController#index
Expected /Users/shamoon/Sites/good_comments/ app/models/comment.rb 定义 Comment Rails.root:/Users/shamoon/Sites/good_comments/test/dummy
有帮助吗?
I'm trying to create a Rails 3 Engine using MongoMapper. I'm having a world of pain getting it going. Here is my model:
module GoodComments
class Comment
include MongoMapper::Document
key :comment, String
end
end
Super simple, I know! My config/routes.rb:
GoodComments::Engine.routes.draw do
resources :comments
end
I created a config/application.rb:
require File.expand_path('../boot', __FILE__)
module GoodComments
class Application < Rails::Application
config.generators do |g|
g.orm :mongo_mapper # :active_record
g.template_engine :erb # :haml
g.test_framework :rspec, :fixture => true, :views => false
g.fixture_replacement :factory_girl, :dir => "spec/factories"
end
end
end
I ran rails generate scaffold_controller Comment -o mongo_mapper
and my controllers were generated. When I run my server and go to http://localhost:3000/good_comments/comments
, I get an error:
LoadError in GoodComments::CommentsController#index
Expected /Users/shamoon/Sites/good_comments/app/models/comment.rb to define Comment
Rails.root: /Users/shamoon/Sites/good_comments/test/dummy
Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您的控制器期望在 comment.rb 中有一个名为 Comment 的类,所以控制器可能需要在同一模块中运行?或者您只需指定一些非默认配置,或更具体地说明控制器应使用哪个模型。
另外,在我的 MongoMapper 应用程序中,我在 config/application.rb 顶部添加的行数比您添加的行数多:
It looks like your controller was expecting a class called Comment in comment.rb, so maybe the controller needs to be operating in the same module? Or you would just have to specify some non-default configurations or be more specific about which model the controller should use.
Also in my MongoMapper app I have a few more lines than you added to the top of config/application.rb: