Rails 4,定制宝石与发电机'找不到发电机'

时间:2014-06-01 08:04:12

标签: ruby-on-rails gem generator

我正在构建一个gem只是为了完成这些过程,我正在尝试为它添加一个生成器。当我运行$ rails g时,我的生成器显示出来:

Mygem:
  mygem:install

但是rails无法识别它

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

我的gem指向我的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

2 个答案:

答案 0 :(得分:11)

我们just made a gem,并有一个生成器,可让您拨打rails generate exception_handler:installlike this

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

这应该对你有帮助 - 它对我们有用

答案 1 :(得分:1)

我在发布我的第一个公共gem后遇到了这个问题,这是因为捆绑器,或者说Rails应用程序无法访问带生成器的实际存储库(我相信如此)。

我一直包括gem 'spree_custom_checkout'

在输入rails g时看到宝石的生成器没有运气。

然后我这样做了:

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

和bam发电机出现了!

步骤进行:

  1. 我做了'gem build spree_custom_checkout.gemspec。
  2. 将它添加到github仓库,并推送到repostiory。
  3. 链接到存储库。