【发布时间】:2016-12-02 22:29:40
【问题描述】:
我正在尝试设置一个 rails 生成器,这是我的第一个,在过去的两个小时里,我一直被困在一个非常简单的事情上 - 如何让用户输入生成器的名称。这是在应用程序中,而不是宝石。
那么在下面的情况下 - 我如何让 'Foo' 在生成器代码上打印?
rails g block Foo
class BlockGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
puts #Foo (file name)#
end
我已经尝试过 NamedBase 和 base 生成器以及我能找到的所有方法。
任何帮助将不胜感激!
# 编辑$ rails g block Foo
class BlockGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
argument :generator_name, type: :string
puts #Foo (file name)#
end
#result
block
No value provided for required arguments 'generator_name'
$ rails generate block :generator_name => testing
#result
is empty, nothing is printed to the console.
【问题讨论】:
-
您应该能够指定将映射到用户输入的参数。您可以在
source_root行之后插入它,如下所示:argument :generator_name, type: :string。然后它将在您的生成器中以generator_name的形式提供。希望对您有所帮助! -
感谢您的回复。我已经修改了您对 NamedBase 和 base 的建议,并为参数提供了一个键,但每次它只响应生成器名称作为值。在这种情况下,“阻止”。我已经编辑了问题以进行演示。再次感谢您的意见
标签: ruby-on-rails ruby