在运行`rails generate controller`时跳过创建测试,资产和帮助器的语法是什么?

Cur*_*ind 79 ruby-on-rails generator ruby-on-rails-3

我阅读了帮助并尝试了以下命令来跳过测试,资产和帮助文件的生成

$ bin/rails generate controller home index  --helper false --assets false --controller-specs false --view-specs false   
create- app/controllers/home_controller.rb
        route  get "home/index"
        invoke  erb
        create    app/views/home
        create    app/views/home/index.html.erb
        invoke  rspec
        error  false [not found]
        error  false [not found]
Run Code Online (Sandbox Code Playgroud)

正如您在上面的输出中所注意到的那样,只有controller, routes&views生成了.但最后两行很有意思:

error  false [not found]
error  false [not found]
Run Code Online (Sandbox Code Playgroud)

显然rails似乎不喜欢--option-name false语法.所以这个错误因为我使用了错误的语法?如果是,那么正确的方法是什么?谢谢

Pin*_*nyM 152

尝试使用--no-后跟optionname:

rails generate controller home index  --no-helper --no-assets --no-controller-specs --no-view-specs
Run Code Online (Sandbox Code Playgroud)

如果要在每次运行generator命令时更改默认行为,可以在application.rb文件中配置所需的默认值 - 请参阅如何确保Rails不为视图和帮助程序生成规范测试?.

  • 您也可以使用`skip`而不是`no`,例如`--skip-assets` (3认同)

Kri*_*ris 73

要关闭而不必添加选项:

# application.rb
config.generators.assets = false
config.generators.helper = false
Run Code Online (Sandbox Code Playgroud)


Ari*_*n L 45

它的作用只是API应用程序不需要javascript,stylesheet,views,helpers.要在generator/scaffold中跳过这些文件,请在下面添加以下代码块application.rb

#to skip assets, scaffolds.css, test framework, helpers, view
config.generators do |g|
  g.template_engine nil #to skip views
  g.test_framework  nil #to skip test framework
  g.assets  false
  g.helper false
  g.stylesheets false
end
Run Code Online (Sandbox Code Playgroud)

有关生成器的更多详细信息,请查看链接

  • 如果您使用“false”而不是“nil”,则上述内容有效。 (2认同)
  • 它可以工作,但是从 Rails 6 开始,使用 false 时您会收到弃用警告,因此最好使用 nil。 (2认同)

Eri*_*man 25

更简洁:

rails g controller home index --no-assets --no-test-framework
Run Code Online (Sandbox Code Playgroud)