Rails 生成器 - 生成器中的多个数组参数返回“未为所需参数提供值”;

发布于 2024-11-09 22:41:10 字数 1491 浏览 4 评论 0 原文

我编写了这个生成器来获取多个数组参数

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}'

但我总是收到错误“没有为所需参数'字段','验证','displays_back','displays_front'提供值”

我真的不知道如何表示数组......有什么建议吗?

ive written this generator to take multiple arrayed arguments

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-11-16 22:41:10

运行查看帮助

rails generate test_scaffold

您可以像这样传递数组选项

rails generate test_scaffold User --fields name age

User 是脚手架名称,可以像这样获取字段

options["fields"]
#=> ["name", "age"]

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"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文