【问题标题】:Rails 3 Generator custom templates with Engine带有引擎的 Rails 3 Generator 自定义模板
【发布时间】:2012-03-02 16:18:26
【问题描述】:
【问题讨论】:
标签:
ruby-on-rails
ruby-on-rails-3
generator
【解决方案1】:
引擎似乎不支持此功能
在 Rails 应用程序中,Finisher 负责将其添加到路径中
module Rails
class Application
module Finisher
include Initializable
initializer :add_generator_templates do
config.generators.templates.unshift(*paths["lib/templates"].existent)
end
......
所以这必须在引擎配置中完成才能使其工作。
module MyEngine
class Engine < ::Rails::Engine
config.generators.templates.unshift File.expand_path("lib/templates", root)
end
end
这是一个错误还是期望的行为?
【解决方案3】:
如果您在 Rails 3.2 引擎的根路径中使用 rails g generator MyGenerator,您将得到如下内容:
class MyGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
end
它不会污染您的 Engine 类,并且更加本地化到生成器。