NoobException
NoobException

Reputation: 666

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

Upvotes: 11

Views: 5671

Answers (4)

Alex Antonov
Alex Antonov

Reputation: 15216

The part that I missed was require "lib/generators/my_gem/install_generator" in my_gem.rb

Upvotes: 0

stevec
stevec

Reputation: 52967

I was having similar trouble because I had used capital letters in my gem name. There's some great documentation about how to name gems here, but the most relevant bit:

Don't use uppercase letters - OS X and Windows have case-insensitive filesystems by default. Users may mistakenly require files from a gem using uppercase letters which will be non-portable if they move it to a non-windows or OS X system. While this will mostly be a newbie mistake we don’t need to be confusing them more than necessary.

I recreated the gem with lowercase and the problem went away.

Upvotes: 0

0bserver07
0bserver07

Reputation: 3471

I faced this issue after publishing my first public gem, it's because the bundler, or let's say the Rails app don't have access to the actual repository with the generator (I believe so).

I kept including gem 'spree_custom_checkout'

no luck to see the gem's generator when typing rails g.

Then I did this:

gem 'spree_custom_checkout', github: '0bserver07/Spree-Custom-Checkout'

and bam the generator shows up!

steps:

  1. I did `gem build spree_custom_checkout.gemspec.
  2. add it to the github repo, and push to the repostiory.
  3. link to the repository.

Upvotes: 1

Richard Peck
Richard Peck

Reputation: 76774

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

Upvotes: 13

Related Questions