如何在Rails引擎中创建问题?

Dag*_*osi 5 ruby-on-rails-plugins ruby-on-rails-3

我正在尝试在引擎内部创建一个问题,以便在将要安装此引擎的主应用程序中添加/覆盖此功能.问题是我遇到了问题,包括引擎模块中的问题.似乎Rails找不到它.

这是我的post.rb文件app/models/blorgh/post.rb:

module Blorgh
  class Post < ActiveRecord::Base
    include Blorgh::Concerns::Models::Post
  end
end
Run Code Online (Sandbox Code Playgroud)

这是我post.rb关心的问题lib/concerns/models/post.rb:

要求'active_support/concern'

module Concerns::Models::Post
  extend ActiveSupport::Concern

  # 'included do' causes the included code to be evaluated in the
  # conext where it is included (post.rb), rather than be 
  # executed in the module's context (blorgh/concerns/models/post).
  included do
    attr_accessible :author_name, :title, :text
    attr_accessor :author_name
    belongs_to :author, class_name: Blorgh.user_class
    has_many :comments

    before_save :set_author

    private
    def set_author
      self.author = User.find_or_create_by_name(author_name)
    end
  end

  def summary
    "#{title}"
  end

  module ClassMethods
    def some_class_method
      'some class method string'
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

当我运行测试/假人时,我得到了这个错误:未初始化的常量Blorgh :: Concerns

这是我的blorgh.gemspec:

$:.push File.expand_path("../lib", __FILE__)

# Maintain your gem's version:
require "blorgh/version"


# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
  s.name        = "blorgh"
  s.version     = Blorgh::VERSION
  s.authors     = ["***"]
  s.email       = ["***"]
  s.homepage    = "***"
  s.summary     = "Engine test."
  s.description = "Description of Blorgh."

  s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
  s.test_files = Dir["test/**/*"]

  s.add_dependency "rails", "~> 3.2.8"
  s.add_dependency "jquery-rails"

  s.add_development_dependency "sqlite3"
end
Run Code Online (Sandbox Code Playgroud)

有人可以帮我弄这个吗?

Mik*_*keS 0

发生这种情况是因为在 Rails 3 中,不会自动查看 lib 目录来查找类。您可以更新 config.autoload_paths 以将 lib 目录添加到引擎中,也可以将关注点/模型/post.rb 从 lib 目录移出并移动到 app/models 中,在那里它将被自动找到。