Rails7でミニアプリ作るときのセットアップ

セットアップ

mkdir アプリの名前
# ディレクトリを作成

cd アプリの名前
# 作業ディレクトリを移動

ruby -v
# Rubyのバージョンを確認

bundle init
# Gemfileを作成
# gem 'rails'と修正

bundle install

rails new . -d postgresql -j esbuild --skip-test
# データベースを postgresqlに指定
# JSバンドラーを esbuild
# 何も指定しない場合、importmapになる
# テストユニットの作成をスキップ

オプション 意味
-d/ --database=mysql データベースの指定。 mysql とか postgresql などを指定する
-skip-test testを作成しない。RSpecに変更したいときに使うと良い。

rails new するときによく使うオプション - Hack Your Design!

https://qiita.com/kyntk/items/0936598a903ac74e607d

gemファイルに以下を追加し bundle install

gem 'cssbundling-rails'

tailwindをインストール

bundle exec rails css:install:tailwind

daisyUIをインストール場合

https://daisyui.com/

yarn add daisyui

tailwind.config.jsを修正

module.exports = {
  content: [
    './app/views/**/*.html.erb',
    './app/helpers/**/*.rb',
    './app/assets/stylesheets/**/*.css',
    './app/javascript/**/*.js'
  ],
  plugins: [
    require("daisyui"),
  ],
}

データベースを作成

bundle exec rails db:create

</details>

gemの導入

group :development, :test do
 :
  gem 'bullet'
  gem 'pry-byebug'
  gem 'pry-rails'
  gem 'rspec-rails'
  gem 'factory_bot_rails'
  gem 'rubocop'
  gem 'rubocop-rails'
end
group :development do
	:

  gem 'better_errors'
  gem 'binding_of_caller'
end

gem 'annotate'
gem 'enum_help'
# 日本語化、多言語化対応
gem 'rails-i18n'

<details><summary>各gemのgithub</summary>

https://github.com/flyerhzm/bullet

https://github.com/BetterErrors/better_errors

https://github.com/banister/binding_of_caller

https://github.com/ctran/annotate_models

https://github.com/thoughtbot/factory_bot_rails

</details>

検索

https://github.com/activerecord-hackery/ransack

ページネーション

https://github.com/kaminari/kaminari

ファイルアップロード

https://github.com/carrierwaveuploader/carrierwave

https://railsguides.jp/active_storage_overview.html

デコレーター

https://github.com/amatsuda/active_decorator

https://github.com/drapergem/draper

使い方

https://nekorails.hatenablog.com/entry/2018/10/13/070437

ユーザー認証(ログイン、ログアウト等)

https://github.com/heartcombo/devise

https://github.com/Sorcery/sorcery

SEO

https://github.com/kpumuk/meta-tags

環境変数を管理

https://github.com/bkeepers/dotenv

https://pikawaka.com/rails/dotenv-rails

参考

https://blog.to-ko-s.com/recomended-gems/

https://qiita.com/tikaranimaru/items/d5b42fa78772cbdcc192

https://qiita.com/tikaranimaru/items/689230e01a7b1af7fb4a

rubocopの設定

https://qiita.com/piggydev/items/074e020e07af7ebc872d

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

タイムゾーンなどの設定

config.time_zone = 'Asia/Tokyo'
config.active_record.default_timezone = :local
config.i18n.default_locale = :ja
config.i18n.load_path += Dir[Rails.root.join('config', 'locales','**', '*.{rb,yml}').to_s]

RSpecの設定

bundle exec rails generate rspec:install

# rails_helperを読み込む
--require rails_helper
# 出力結果をドキュメント風にして見やすくする
--format documentation
# 出力結果を色分けする
--color

# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'factory_bot'  # 追加
require 'rspec/rails'

# コメントアウト解除
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }

begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  puts e.to_s.strip
  exit 1
end
RSpec.configure do |config|
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!

  config.include FactoryBot::Syntax::Methods  # 追加
end


# コメントアウト解除
config.filter_run_when_matching :focus

annotate

bundle exec rails g annotate:install
bundle exec annotate

lib/tasks/auto_annotate_models.rake が作成される

https://techracho.bpsinc.jp/ikeda-kazuyuki/2014_08_29/18876

bullet

Rails.application.configure do
	#ここから追加
  config.after_initialize do
    Bullet.enable        = true
    Bullet.alert         = true
    Bullet.bullet_logger = false
    Bullet.console       = false
    Bullet.rails_logger  = true
    Bullet.add_footer    = true
  end
:

Bullet.enable bulletを有効
Bullet.alert JavaScriptアラートをポップアップで表示
Bullet.bullet_logger bullet用のログファイルをlog/bullet.logに出力
Bullet.console bulletがN+1を検出した際にブラウザのconsole.logにメッセージを出力
Bullet.growl Growlがインストールされている場合、Growlメッセージを表示
Bullet.rails_logger bulletがN+1を検出した際にrailsログを出力
Bullet.add_footer ブラウザで表示しているページの左下隅にN+1検出情報を表示

【Rails】N+1問題をアラート表示してくれるgem「bullet」を初心者向けにまとめてみた|TechTechMedia

その他

OGP画像設定

https://zenn.dev/yoshiki105/articles/eb093bf603e728

作成はこちらのサイトを使用している人が多い

https://www.canva.com/

Google Analytics

https://zenn.dev/yoiyoicho/articles/1fe05797a1bbc4