ruby-on-rails – Rails 4,带发电机的定制宝石’找不到发电机’

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails 4,带发电机的定制宝石’找不到发电机’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建一个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

解决方法

我们有 just made a gem,并且有一个生成器允许你调用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

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

猜你在找的Ruby相关文章