Rails Generators- Multiple array arguements in generator returning 'No value provided for required arguments'
ive written this generator to take multiple arrayed arguments
class TestScaffoldGenerator < Rails::Generators::NamedBase
#tem开发者_如何学JAVAplate 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
When i run 'rails g test_scaffold' i dont know how to denote the end of an arrayed argument and the begining of another... ive tried and many more:
'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}'
But i always get the error "No value provided for required arguments 'fields', 'validations', 'displays_back', 'displays_front'"
im really not sure how to denote an array... any suggestions?
Run to see the help
rails generate test_scaffold
You can pass array option like this
rails generate test_scaffold User --fields name age
User is the scaffold name and fields can be fetched like this
options["fields"]
#=> ["name", "age"]
精彩评论