我写了这个生成器来获取多个数组参数
class TestScaffoldGenerator < Rails::Generators::NamedBase
#template source
source_root File.expand_path("../templates", __FILE__)
#scaffold_name argument
argument :fields, :type => :array, :required => true, :desc => "required"
argument :validations, :type => :array, :required => true, :desc => "required"
argument :displays_back, :type => :array, :required => true, :desc => "required"
argument :displays_front, :type => :array, :required => true, :desc => "required"
#gem_dependancies
gem("haml")
#initial generator method
def generate_test_scaffold
template "model.rb.erb", "app/models/#{file_name}.rb"
end
#private methods do get run when generator is initiated
private
#scaffold_name to underscore gives filename
def file_name
class_name.demodelize
end
end
当我运行'rails g test_scaffold'时,我不知道如何表示阵列参数的结束和另一个的开始...... 我尝试了很多:
'rails g test_scaffold name:string string black white'
'rails g test_scaffold name:string, string, black, white'
'rails g test_scaffold [name:string] [string] [black] [white]'
'rails g test_scaffold [name:string], [string], [black], [white]'
'rails g test_scaffold {name:string} {string} {black} {white}'
'rails g test_scaffold {name:string}, {string}, {black}, {white}'
但我总是得到错误“没有为所需的参数'字段提供值','验证','display_back','displays_front'”
我真的不确定如何表示数组...有什么建议吗?
答案 0 :(得分:1)
运行以查看帮助
rails generate test_scaffold
您可以像这样传递数组选项
rails generate test_scaffold User --fields name age
用户是脚手架名称,可以像这样获取字段
options["fields"]
#=> ["name", "age"]