2017-03-12 134 views
-1

在我的寶石,我使用以下發電機:Rails的:未定義的局部變量或方法'選項

require 'rails/generators/base' 
require 'thor' 

module Gem 
    module Generators 

     class ViewsGenerator < Rails::Generators::Base 

      class_option :split, desc: "ABC", type: :boolean, default: false 

      unless options[:split] 
       ... 
      else 
       ... 
      end 

     end 

    end 
end 

旁註:它可以添加Command Line Arguments。 +它工作正常,沒有if條件。


例外我越來越:

[WARNING] Could not load generator "generators/amphtml/views_generator". Error: undefined local variable or method options' for Amphtml::Generators::ViewsGenerator:Class. c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/amphtml-0.1.4/lib/generators/amphtml/views_generator.rb:15:inclass:ViewsGenerator' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/amphtml-0.1.4/lib/generators/amphtml/views_generator.rb:7:in ' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/amphtml-0.1.4/lib/generators/amphtml/views_generator.rb:5:inmodule:Amphtml' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/amphtml-0.1.4/lib/generators/amphtml/views_generator.rb:4:in ' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/activesupport-5.1.0.beta1/lib/active_support/dependencies.rb:292:inrequire' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/activesupport-5.1.0.beta1/lib/active_support/dependencies.rb:292:in block in require' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/activesupport-5.1.0.beta1/lib/active_support/dependencies.rb:258:inload_dependency' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/activesupport-5.1.0.beta1/lib/active_support/dependencies.rb:292:in require' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/command/behavior.rb:82:inblock (2 levels) in lookup' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/command/behavior.rb:78:in each' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/command/behavior.rb:78:inblock in lookup' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/command/behavior.rb:77:in each' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/command/behavior.rb:77:inlookup' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/generators.rb:250:in find_by_namespace' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/generators.rb:267:ininvoke' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/commands/generate/generate_command.rb:19:in perform' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/command.rb:27:inrun' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/invocation.rb:126:in invoke_command' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor.rb:369:indispatch' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/command/base.rb:63:in perform' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/command.rb:44:ininvoke' c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/commands.rb:16:in ' bin/rails:4:inrequire' bin/rails:4:in `


我不知道發生了什麼事情。

+0

我不知道你的問題是什麼。 – sawa

+0

@sawa根據鏈接的文檔,在提供的代碼片段中應該記錄生成器的命令行參數。 **問題:**爲什麼該片段無法正常工作? – jonhue

回答

1

你需要用你的條件在被調用發電機時調用方法:

def new(options) 
    unless options[:split] 
    ... 
    else 
    ... 
    end 
end 

嘗試this resource使用雷神打造發電機

+0

獲取以下錯誤:''handle_argument_error':rails new需要1個參數,但它不應該。 (引發ArgumentError)' – jonhue

相關問題