Rails 自動生成ファイル設定
Rails では rails generate xxx
コマンドを使ってコントローラ、モデルなどの動作に必要なファイルを自動で生成できます。
しかし、CSS や JavaScript ファイル、ヘルパーファイルなどは使わない場合もあるので、自動で生成されてしまうと消すのが面倒です。
そこで、一部ファイルについては rails generate
コマンドを実行しても自動で生成されないように設定していきます。
config.generators 設定
設定するファイルは、既存の /config
ディレクトリにある application.rb
というファイルです。
変更する前は以下のような内容が書かれています。
config/application.rb
require_relative "boot"
require "rails/all"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Saigen
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 7.0
# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
# in config/environments, which are processed later.
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
end
end
まずは、見通しをよくするために一旦コメントアウトの部分は削除してしまいます。
config/application.rb (コメントアウト部分削除後)
require_relative "boot"
require "rails/all"
Bundler.require(*Rails.groups)
module TechLogApp
class Application < Rails::Application
config.load_defaults 7.0
end
end
その後、class Application
の中に以下の 4 行を追記します。
class Application < Rails::Application
config.load_defaults 7.0
config.generators do |g| # ここから追記
g.assets false # CSS, JavaScriptファイルを自動生成しない
g.helper false # helperファイルを自動生成しない
end # ここまで追記
end
以上の 4 行で設定は完了です。
変更をコミット
ここまでの変更をコミット、およびプッシュしておきます。
$ git add .
$ git commit -m "アセットファイル等はrails generate時に自動生成されないように設定"