2
1

More than 5 years have passed since last update.

Rails::Generators の #namespace がフォルダ名にも影響していて、ハマった話。

Last updated at Posted at 2013-04-08

モジュール名がHOGE::Generators::ConfigGenerator の時自動的に小文字変換されるので、

$ rails g h_o_g_e:config

となってしまう。

なので、namespaceを指定してやる必要がある。

module HOGE
  module Generators
    class ConfigGenerator < Rails::Generators::Base
      source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
      namespace "hoge:config" # <- THIS!!

      def copy_config_file
        template 'hoge_config.rb', 'config/initializers/hoge_config.rb'
      end
    end
  end
end

この時、namespaceがフォルダ名にも影響してくるので、フォルダ名は

lib/generators/hoge/templates/hoge_config.rb

でなければならない。

色々試している時に、フォルダ名を

lib/generators/h_o_g_e/templates/config_generator.rb

としていたために、エラーが発生しハマった。source_root で指定してるパスからテンプレートも拾ってくれると思ってたんだが…

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1