Вопрос

I have a problem when creating a generator inside a gem. When I run rails g the generator is shown:

Supportator:
  supportator:initializer

But when I run the generator with rails generate supportator:initializer the following error occurs:

Could not find generator supportator:initializer.

This is the code of the generator:

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

This is the code of the engine:

module Supportator
  require 'rails'
  class Engine < ::Rails::Engine
  end
end

Do you know why this is happening?

Это было полезно?

Решение

Change

lib/generators/supportator/supportator_generator.rb

to

lib/generators/supportator/initializer_generator.rb 

Your class name is InitializerGenerator so file name should be initializer_generator.rb. Otherwise, rails would not be able to find it.

In your case with rails g supportator:initializer, rails would look for a generator file initializer_generator.rb in lib/generators/supportator directory.

Другие советы

The problem was the directory and the name of the file. To the following generator:

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

The directory and the file name should be:

/lib
  /generators
    /supportator
      /install
        /install_generator.rb
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top