使用 MongoMapper 创建 Rails 引擎

发布于 2024-12-25 22:38:26 字数 1198 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪也成诗 2025-01-01 22:38:26

看起来您的控制器期望在 comment.rb 中有一个名为 Comment 的类,所以控制器可能需要在同一模块中运行?或者您只需指定一些非默认配置,或更具体地说明控制器应使用哪个模型。

另外,在我的 MongoMapper 应用程序中,我在 config/application.rb 顶部添加的行数比您添加的行数多:

require File.expand_path('../boot', __FILE__)

# from http://mongomapper.com/documentation/getting-started/rails.html
# replace:
# require 'rails/all'
# with:
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
# Uncomment for asset pipelining in Rails 3.1
# require "sprockets/railtie"

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:

require File.expand_path('../boot', __FILE__)

# from http://mongomapper.com/documentation/getting-started/rails.html
# replace:
# require 'rails/all'
# with:
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
# Uncomment for asset pipelining in Rails 3.1
# require "sprockets/railtie"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文