【问题标题】:Creating a Rails Engine with MongoMapper使用 MongoMapper 创建 Rails 引擎
【发布时间】:2012-01-10 23:28:30
【问题描述】:

我正在尝试使用 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

我运行了rails generate scaffold_controller Comment -o mongo_mapper 并生成了我的控制器。当我运行我的服务器并转到 http://localhost:3000/good_comments/comments 时,我收到一个错误:

GoodComments::CommentsController#index 中的加载错误

预期 /Users/shamoon/Sites/good_cmets/app/models/comment.rb 来定义评论 Rails.root: /Users/shamoon/Sites/good_cmets/test/dummy

有什么帮助吗?

【问题讨论】:

  • 好的.. 如果我摆脱模型中的 GoodComments 命名空间,它会起作用。但是脚手架new 不会读取模型定义中的字段。

标签: ruby-on-rails-3 mongomapper


【解决方案1】:

看起来您的控制器在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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多