[Ruby] Restrict files created by rails g controller command

less than 1 minute read

Development environment

rails 6.0.3 (Assuming a project that does not use Minitest)

Explanation

rails g controller <controller name> <action name> When you execute, a file that is not particularly needed is created in addition to controller and view.

To avoid this, create the following file in the rails project, When executing the rails g controller command, Files other than controller and view will not be created **.

config/initializers/generators.rb


Rails.application.config.generators do |g|
  g.assets false
  g.helper false
  g.skip_routes true
end

If creating the above file is troublesome, Execute the rails g controller command with the following three options added.

terminal


rails g controller <controller name> <action name> --no-assets --no-helper --skip-routes


The above has the following advantages.

  • The stylesheets file is not created under app/assets/**.
  • Helper file is not created under app/helpers/.
  • Routes are not added in config/routes.rb.