🦓

[Rails]よくやる初期設定をまとめてみた

2023/07/28に公開

はじめに

rails newをするたびによくやる初期設定をまとめてみました。
2023年8月時点のものになりますので情報が古い可能性があります。

DBをpostgresqlにする

rails new xxx —database=postgresql

application.rb

  • タイムゾーンの設定
  • DBのタイムゾーンの設定
  • ジェネレーターの設定
  • 言語を日本語にする
  • ローケルファイルのパスを設定する
config/application.rb
class Application < Rails::Application
    config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]
    config.i18n.default_locale = :ja
    config.time_zone = 'Tokyo'
    config.active_record.default_timezone = :local

    config.generators do |g|
      g.helper false
      g.test_framework false
      g.assets false
      g.skip_routes false
      g.factory_bot false
    end
end

development.rb

  • アセットのディバッグをtrueにする
  • メーラーの設定
config/environments/development.eb
Rails.application.configure do
  config.assets.debug = true

  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
  config.action_mailer.delivery_method = :letter_opener_web
  config.action_mailer.perform_deliveries = true
end

Gemfile

よく使うgemをインストールします。

Gemfile
group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'letter_opener_web'
  gem 'faker'
  gem 'factory_bot_rails'
  gem 'dotenv-rails'
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 4.1.0'
  # Display performance information such as SQL time and flame graphs for each request in your browser.
  # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
  gem 'rack-mini-profiler', '~> 2.0'
  gem 'listen', '~> 3.3'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 3.26'
  gem 'selenium-webdriver', '>= 4.0.0.rc1'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
end

application_controller.rb

デフォルトの言語を日本にします。

app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
    before_action :set_locale

    private

    def set_locale
        I18n.locale = current_user&.locale || :ja
    end
end

bin/devを使う

foremanが必要となるのでない場合インストールさせます。

bin/dev
#!/usr/bin/env sh

if ! gem list foreman -i --silent; then
  echo "Installing foreman..."
  gem install foreman
fi

exec foreman start -f Procfile.dev "$@"

Procfile.devを作成する

tailwindを使う場合ウォッチするコマンドを追加します。

Procfile.dev
web: bin/rails server -p 3000
css: bin/rails tailwindcss:watch

rspecを使う

rspecの導入と設定を行います。
こちらの記事をご参考にしてみてください。
https://zenn.dev/redheadchloe/articles/20c3375b328e09

Dockerを使う

こちらの記事をご参考にしてみてください。
https://zenn.dev/redheadchloe/articles/2d647285650521

.gitignore

.gitignoreを作成します。
こちらを参考にし、アプリの要件に合わせて作成します。
https://github.com/github/gitignore/blob/main/Rails.gitignore

bin/rails about

アプリの環境を確認します。

➜ bin/rails about
About your application's environment
Rails version             7.0.6
Ruby version              ruby 3.2.1 (2023-02-08 revision 31819e82c8) [x86_64-darwin22]
RubyGems version          3.4.6
Rack version              2.2.7
Middleware                ActionDispatch::HostAuthorization, Rack::Sendfile, ActionDispatch::Static, ActionDispatch::Executor, ActionDispatch::ServerTiming, ActiveSupport::Cache::Strategy::LocalCache::Middleware, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, ActionDispatch::RemoteIp, Sprockets::Rails::QuietAssets, Rails::Rack::Logger, ActionDispatch::ShowExceptions, WebConsole::Middleware, ActionDispatch::DebugExceptions, ActionDispatch::ActionableExceptions, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ContentSecurityPolicy::Middleware, ActionDispatch::PermissionsPolicy::Middleware, Rack::Head, Rack::ConditionalGet, Rack::ETag, Rack::TempfileReaper, Warden::Manager
Application root          /Users/***/***/***
Environment               development
Database adapter          postgresql
Database schema version   20230728065038

終わり

よくやる初期設定をまとめてみました。
誰かの参考になれば嬉しいです。

https://zenn.dev/ryouzi/articles/1f947d291f4baa
https://guides.rubyonrails.org/configuring.html

Discussion