在我的宝石中,我使用以下生成器:
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
条件。
我得到的例外:
[警告]无法加载发电机 &#34;发电机/ amphtml / views_generator&#34 ;.错误:未定义的局部变量 或方法选项&#39; 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&#39; C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/amphtml-0.1.4/lib/generators/amphtml/views_generator.rb:7:在 &#39; 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&#39; C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/amphtml-0.1.4/lib/generators/amphtml/views_generator.rb:4:在 &#39; C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/activesupport-5.1.0.beta1/lib/active_support/dependencies.rb:292:inrequire&#39; C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/activesupport-5.1.0.beta1/lib/active_support/dependencies.rb:292:在 阻止需要&#39; 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&#39; C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/activesupport-5.1.0.beta1/lib/active_support/dependencies.rb:292:在 需要&#39; 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 level)in lookup&#39 ; C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/command/behavior.rb:78:在 每个&#39; 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&#39; C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/command/behavior.rb:77:在 每个&#39; C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/command/behavior.rb:77:inlookup&#39; C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/generators.rb:250:在 find_by_namespace&#39; C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/generators.rb:267:ininvoke&#39; 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:在 执行&#39; C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/command.rb:27:inrun&#39; c:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/invocation.rb:126:in invoke_command&#39; C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor.rb:369:indispatch&#39; C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/command/base.rb:63:在 执行&#39; C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/command.rb:44:ininvoke&#39; C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.1.0.beta1/lib/rails/commands.rb:16:在 &#39;箱/轨道:4:inrequire&#39; bin / rails:4:在`
我不知道发生了什么。
答案 0 :(得分:1)
您需要将条件包装在调用生成器时调用的方法中:
def new(options)
unless options[:split]
...
else
...
end
end
尝试this resource使用Thor构建生成器