How to override a rails generator template in a gem?

后端 未结 6 1200
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-01 07:18

When you want to override a generator template (without replacing the generator itself), in Rails 3 you can just drop files in appropriately named places in lib/templates and Ra

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 07:51

    Update: IdahoEv has the right answer, but this code, as of 3.1 produces the following deprication warning:

    DEPRECATION WARNING: config.generators in Rails::Railtie is deprecated. Please use config.app_generators instead.
    

    So use this instead:

    module MyGem
      class Railtie < Rails::Railtie
        config.app_generators do |g|
          g.templates.unshift File::expand_path('../templates', __FILE__)
        end 
      end
    end 
    

提交回复
热议问题