NoobException NoobException - 4 months ago 31
Ruby Question

Rails 4, custom gem with generator 'Could not find generator'

I'm building a gem simply to go through the processes, and I'm trying to add a generator to it. When I run

$ rails g
, my generator shows up:

Mygem:
mygem:install


but rails doesn't recognize it

rails g mygem:install
Could not find generator mygem:install.


I have my gem pointed to the latest version in my gemfile

#mygem/lib/rails/generators/mygem_generator.rb
require 'rails/generators'
require 'rails/generators/base'

module Mygem
class InstallGenerator < Rails::Generators::Base
def test
puts 'hi'
end
end
end


.

-mygem
- lib
- generators
- mygem
mygem_generator.rb
- mygem
version.rb
mygem.rb
- pkg
mygem-0.0.1.gem
.gitignore
Gemfile
LICENSE.txt
README.md
Rakefile
mygem.gemspec


.

#mygem.gemspec
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'mygem/version'

Gem::Specification.new do |spec|
spec.name = "mygem"
spec.version = Mygem::VERSION
spec.authors = ["me"]
spec.email = ["email@email.com"]
spec.summary = %q{lalaala}
spec.description = %q{lalalalal}
spec.homepage = ""
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.5"
spec.add_development_dependency "rake"
end

Answer

We've just made a gem, and have a generator which allows you to call rails generate exception_handler:install, like this:

#lib/generators/my_gem/install_generator.rb
module MyGem
    class InstallGenerator < Rails::Generators::Base
         def test
             puts "hi"
         end
     end
end

This should help you - it works for us