my_zipcode_gem迁移生成器无法正常工作

时间:2015-06-21 12:55:28

标签: ruby-on-rails ruby

我很确定这个问题是由于这个宝石看起来已经有3到3年了,导轨已经改变了我们的迁移方式。但我不太熟悉宝石/发电机。我正在尝试按照说明执行here列出的说明。

rails g my_zipcode_gem:models
rake db:migrate
rake zipcodes:update

但是,当我完成第一步时,我最终得到了这个:

/Users/thammond/.rvm/gems/ruby-2.1.1/gems/my_zipcode_gem-0.1.3/lib/generators/my_zipcode_gem/models_generator.rb:35:in `create_migration': wrong number of arguments (3 for 0) (ArgumentError)
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/generators/migration.rb:63:in `migration_template'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/my_zipcode_gem-0.1.3/lib/generators/my_zipcode_gem/models_generator.rb:36:in `create_migration'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `block in invoke_all'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `each'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `map'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `invoke_all'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/group.rb:232:in `dispatch'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/generators.rb:157:in `invoke'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/generate.rb:11:in `<top (required)>'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in `require'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in `block in require'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in `require'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:135:in `generate_or_destroy'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:51:in `generate'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

知道我需要做些什么来让宝石工作?它提供了我正在寻找的应用程序。

谢谢!

编辑:添加代码:

module MyZipcodeGem
  class ModelsGenerator < Base
    include Rails::Generators::Migration

    source_root File.expand_path('../templates', __FILE__)

    def initialize(*args, &block)
      super
    end

    def generate_models
      # puts ">>> generate_zipcodes:"
    end

    def add_gems
      add_gem "mocha", :group => :test
    end

    def create_models
      template 'zipcode_model.rb', "app/models/zipcode.rb"
      template 'county_model.rb', "app/models/county.rb"
      template 'state_model.rb', "app/models/state.rb"
    end

    # Implement the required interface for Rails::Generators::Migration.
    # taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
    def self.next_migration_number(dirname)
      if ActiveRecord::Base.timestamped_migrations
        Time.now.utc.strftime("%Y%m%d%H%M%S")
      else
        "%.3d" % (current_migration_number(dirname) + 1)
      end
    end

    def create_migration
      migration_template 'migration.rb', "db/migrate/create_my_zipcode_gem_models.rb"
    end

    def create_rakefile
      template 'zipcodes.rake', "lib/tasks/zipcodes.rake"
    end

  end
end

# /Users/cblackburn/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/rails/generators/active_record/

1 个答案:

答案 0 :(得分:2)

看起来它正在使用不再需要的参数调用迁移。您有两种选择:

1)fork代码,更新它以使用rails 4约定,并在github上创建一个pull请求。假设所有者仍处于活动状态,他们会将您的代码带入主服务器并重新发布gem。你会得到帮助支持社区,并在此过程中感觉良好。像我这样的人可以帮助你解决这个问题。

2)找到另一个宝石来做你想做的事情,比如http://www.rubygeocoder.com/,我曾经多次使用过,并且在处理位置/邮政编码问题时非常棒