Ruby-on-rails 跳过为“rails generate controller”创建测试、资产和帮助程序的语法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14045858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:13:10  来源:igfitidea点击:

Syntax to skip creating tests, assets & helpers for `rails generate controller`?

ruby-on-railsruby-on-rails-3generator

提问by CuriousMind

I read the help & tried the following command to skip generation of tests, assets & helper files

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

$ 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]

As you may notice by output above this works & only controller, routes& viewsare generated. But as last two lines are interesting:

正如您可能通过上面的输出注意到的那样,这有效且仅生成controller, routes& views。但最后两行很有趣:

error  false [not found]
error  false [not found]

Obviously rails doesn't seem to like --option-name falsesyntax. so this this error because I used the wrong syntax? If yes, then what is the correct way? Thanks

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

回答by PinnyM

Try using --no-followed by optionname:

尝试使用--no-后跟optionname

rails generate controller home index  --no-helper --no-assets --no-controller-specs --no-view-specs

If you want to change the default behavior every time you run the generator command, you can configure the defaults you would like in the application.rb file - see How can I make sure Rails doesn't generate spec tests for views and helpers?.

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

回答by Kris

To turn off without having to add options:

要关闭而不必添加选项:

# application.rb
config.generators.assets = false
config.generators.helper = false

回答by Arivarasan L

Applications which serve only API will not require javascript, stylesheet, views, helpers. To skip those files in generator/scaffold add the below code block in the application.rb

仅提供 API 的应用程序不需要javascriptstylesheetviewshelpers。要跳过生成器/脚手架中的这些文件,请在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

check the link for more details about generators

检查链接以获取有关生成器的更多详细信息

回答by Erik Trautman

More concisely:

更简洁:

rails g controller home index --no-assets --no-test-framework

回答by vidur punj

Inside application.rb file write: This will stop generating everything apart from what is written in the command line

内部 application.rb 文件写入:这将停止生成除命令行中写入的内容之外的所有内容

config.generators do |g|
  g.test_framework nil
  g.template_engine nil
  g.asstes false
  g.helper false
  g.stylesheets false
  g.javascripts false
end

Example:

例子:

vidur@vidur-desktop:~/Downloads/tukaweb$ rails g controller uploader/three_d_models 
Running via Spring preloader in process 3703
      create  app/controllers/uploader/three_d_models_controller.rb
      invoke  assets
      invoke    js
      invoke    scss

for one liner solution =>

对于一种衬垫解决方案 =>

rails g controller assets_garments --skip-test-framework --skip-assets --skip-helper

回答by Jin Lim

If you want to generate only controller, nothing else.

如果你只想生成控制器,别无他物。

rails g controller [controller_name] [index] --no-helper --no-assets --no-template-engine --no-test-framework