ЯoomeR

プログラミング~実装とエラー解決と、時々、AI~

Railsファイル辞典(appディレクトリ編)

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のメーラー版 (テキストメール)