rails newで生成されるファイルが「何のためにあるのか?」を徹底解説。
各ディレクトリの階層を再現している。
※本記事はRailsのver6系統を使用している。
appディレクトリ
assetesディレクトリ
configディレクトリ
manifest.js
プリコンパイルするアセットを指定している。
初期状態では以下のようになっている。
//= link_tree ../images //= link_directory ../stylesheets .css
imagesディレクトリ
.keep
.keepファイルは空のファイルである。
これは、「Gitは中身が空のディレクトリは管理下に置かない」という特性のためである。
つまり、対象のディレクトリをGit管理下に置くために、空のファイルを仮配置している。
以降、.keepファイルの記述は省略する。
stylesheetsディレクトリ
application.css
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's
* vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope.
*
*= require_tree .
*= require_self
*/
*= require_tree .
の記述により、同フォルダ内のcssファイルが読み込まれる。
*= require_self
の記述により、「applicatioin.css内に記述した内容」が読み込まれる。
application.css
内に記述を加えていないのであれば、削除してもビュー情報は変わらない。
cnannelsディレクトリ
Action Cable関連のファイルを配置するディレクトリである。
application_cableディレクトリ
channel.rb
Action Cableのチャネル
connection.rb
Action Cableのコネクション
Controllersディレクトリ
concernsディレクトリ
複数のコントローラーで使用する共通のメソッドを記述するファイルを配置する。
application.controller
このコントローラーに記載したメソッドは、すべてのコントローラーで適用される。
helpersディレクトリ
application.helper
DRYなコードを実現するため、主にViewで共通するメソッドを記述する。
コントローラーやモデルに適用するためにはincludeする必要がある。
javascriptディレクトリ
channelsディレクトリ
consumer.js
Action Cableで使用するコンシューマー
index.js
各channel.jsを読み込むためのファイル
packsディレクトリ
application.js
application.cssと同じ役割。
jobsディレクトリ
Active Jobを実装するためのディレクトリ。
application_job.rb
各ジョブはApplicationJob
を継承する。
class ApplicationJob < ActiveJob::Base
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked
# Most jobs are safe to ignore if the underlying records are no longer available
# discard_on ActiveJob::DeserializationError
end
mailersディレクトリ
Action Mailerを実装するためのディレクトリ
application_mailer.rb
各メーラーはApplicationMailer
を継承する。
class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
layout 'mailer'
end
modelsディレクトリ
concernsディレクトリ
複数のモデルで使用する共通のメソッドを記述するファイルを配置する。
application_record.rb
各モデルはApplicationRecord
を継承する。
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
self.abstract_class = trueは「このテーブル名を探さない」という宣言。
すべてのモデルがApplicationRecordを継承するため、この記述が必要になる。
こちらを削除すると以下のようなエラーが発生する。
Mysql2::Error: Table 'アプリケーション名_development.application_records' doesn't exist
viewsディレクトリ
layoutsディレクトリ
application.html.erb
ビューを呼び出す時、必ずapplication.html.erbが読み込まれる。
各ビューファイルはbody内の<%= yield %>
で読み込まれる。
<!DOCTYPE html>
<html>
<head>
<title>Filecheck</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= yield %>
</body>
</html>
mailer.html.erb
application.html.erbのメーラー版 (HTMLメール)
mailer.text.erb
application.html.erbのメーラー版 (テキストメール)