Is there a way with the usual generators config to turn OFF the creation of the view folders and action templates when you run a rails generate controller
?
It's not a well documented feature, but try to add --skip-template-engine
(alias --no-template-engine
) option to the command.
rails generate controller foo bar --skip-template-engine
demo on a dummy app:
rails g controller my_controller index show --no-template-engine
create app/controllers/my_controller_controller.rb
route get "my_controller/show"
route get "my_controller/index"
invoke test_unit
create test/functional/my_controller_controller_test.rb
invoke helper
create app/helpers/my_controller_helper.rb
invoke test_unit
create test/unit/helpers/my_controller_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/my_controller.js.coffee
invoke scss
create app/assets/stylesheets/my_controller.css.scss
Just thought I'd try underscoring the --skip-template-engine flag to see if it worked in a generator and it worked a charm! No view templates generated from a bin/rails g controller
command in a Rails 4.2 application.
Try:
config.generators do |g|
g.template_engine false
end
A little late I know but these things stick around in Google! ;)
To skip views from being generated with your controller, disable the template engine.
Once:
rails g controller ControllerName action1 action2 --skip-template-engine
Note that every --skip
option also has an aliased --no
option.
Default:
# config/application.rb
config.generators do |g|
g.template_engine false
end
# OR
config.generators.template_engine = false
If you have an API-only application (no front end), you may also want to skip assets and helpers from being generated with your controllers.
Once:
rails g controller api/users --no-helper --no-assets --no-template-engine
Default:
# config/application.rb
config.generators do |g|
g.assets false
g.helper false
g.template_engine false
end
# OR
config.generators.assets = false
config.generators.helper = false
config.generators.template_engine = false
Disabling assets skips stylesheets and javascripts from being generated. If you only want to skip one, use --no-stylesheets
or --no-javascripts
, or in config/application.rb
use:
config.generators.stylesheets = false
config.generators.javascripts = false
If your default configuration skips something from being generated (e.g. assets and helpers) but you need them in one case, you can generate them like so:
rails g controller foo --helper --assets --skip
where --skip
skips generating files that already exist.
If you're creating an API with no front end, you can go ahead and use rails new --api
. However, I don't recommend this option if you do plan to create a front end (for example a single page app) because it turns a lot of things off, including the asset pipeline.