93

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 & views are generated. But as last two lines are interesting:

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

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

6 Answers 6

170

Try using --no- followed by 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?.

1
  • 4
    You can also use skip instead of no, e.g. --skip-assets
    – Dennis
    Jan 21, 2015 at 16:55
78

To turn off without having to add options:

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

Applications which serve only API will not require javascript, stylesheet, views, helpers. To skip those files in generator/scaffold for Rails 3.x add the below code block in the 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

2
  • 2
    The above works if you use false instead of nil.
    – bigtex777
    Jun 20, 2017 at 22:13
  • 2
    It works but you will receive a deprecation warning as of Rails 6 when using false, so it's better to use nil.
    – Gino
    Oct 15, 2020 at 3:42
34

More concisely:

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

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

config.generators do |g|
  g.test_framework nil
  g.template_engine nil
  g.assets 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
2

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.