在gem中创建生成器时遇到问题。当我运行rails g
时,会显示生成器:
Supportator:
supportator:initializer
但是当我使用rails generate supportator:initializer
运行生成器时,会发生以下错误:
Could not find generator supportator:initializer.
这是生成器的代码:
require 'rails/generators'
module Supportator
class InitializerGenerator < Rails::Generators::Base
source_root File.expand_path("../templates", __FILE__)
def create_initializer_file
copy_file '_browser_validator.html.haml', 'app/views/_browser_validator.html.haml'
copy_file 'en_supportator.yml' , 'config/locales/en_supportator.yml'
copy_file 'es_supportator.yml' , 'config/locales/es_supportator.yml'
end
end
end
这是引擎的代码:
module Supportator
require 'rails'
class Engine < ::Rails::Engine
end
end
你知道为什么会这样吗?
答案 0 :(得分:6)
更改
lib/generators/supportator/supportator_generator.rb
到
lib/generators/supportator/initializer_generator.rb
您的班级名称为InitializerGenerator
,因此文件名应为initializer_generator.rb
。
否则,rails将无法找到它。
对于使用rails g supportator:initializer
的情况,rails会在initializer_generator.rb
目录中查找生成器文件lib/generators/supportator
。
答案 1 :(得分:2)
问题是文件的目录和名称。到以下生成器:
require 'rails/generators'
module Supportator
module Generators
class InstallGenerator < ::Rails::Generators::Base
source_root File.expand_path("../../../templates", __FILE__)
def create_initializer_file
copy_file '_browser_validator.html.haml', 'app/views/_browser_validator.html.haml'
copy_file 'en_supportator.yml' , 'config/locales/en_supportator.yml'
copy_file 'es_supportator.yml' , 'config/locales/es_supportator.yml'
end
end
end
end
目录和文件名应为:
/lib
/generators
/supportator
/install
/install_generator.rb