【问题标题】:Rails 3 Generator custom templates with Engine带有引擎的 Rails 3 Generator 自定义模板
【发布时间】:2012-03-02 16:18:26
【问题描述】:

当使用带有 Rails::Engine 的 rails 生成器时,它似乎不会拾取任何放入 lib 目录的模板文件。按照这里的指示

http://guides.rubyonrails.org/generators.html#customizing-your-workflow-by-changing-generators-templates

我现在有

 lib/templates/rails/scaffold_controller

我也试过了

 lib/my_engine/templates/rails/scaffold_controller

有其他人尝试过吗?

【问题讨论】:

    标签: 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
    

    这是一个错误还是期望的行为?

    【讨论】:

      【解决方案2】:

      上面的答案(由 stellard 本人提出)并没有解决我在 Rails 3.2 中的问题,但 How to override a rails generator template in a gem? 解决了它。只是为像我这样的人指出来。

      【讨论】:

        【解决方案3】:

        如果您在 Rails 3.2 引擎的根路径中使用 rails g generator MyGenerator,您将得到如下内容:

        class MyGenerator < Rails::Generators::NamedBase
          source_root File.expand_path('../templates', __FILE__)
        end
        

        它不会污染您的 Engine 类,并且更加本地化到生成器。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-24
          • 1970-01-01
          相关资源
          最近更新 更多