LoginSignup
2
2

脳死 rails new するためのコマンド

Last updated at Posted at 2023-03-26

はじめに

新しいバージョンを試したいときに、
rails new で自分が指定するオプションとかを毎回忘れるのでメモ

環境

  • OS: Mac M1
    • homebrew 利用
    • bundler 利用
    • asdf利用(Rubyのバージョン管理ツールに利用)
  • 作成する Rails アプリケーション
    • 最新安定バージョンのruby
    • 最新安定バージョンのrails
    • テストはRSpec
  • この記事を作成したときのバージョンは下記
    • Ruby: 3.2.2
    • Rails: 7.1.2

本題

RubyとRails の最新安定バージョンをチェック

asdf のバージョンアップ

  1. asdf をバージョンアップして最新のrubyをインストールできるようにする
    brew update && brew upgrade asdf
    
  2. asdf reshim で新しいバージョンのasdfを使うようにする
    asdf reshim
    

ruby をインストール

asdf install インストールしたいバージョン ruby

新しいRailsアプリケーションを作成するディレクトリの設定を更新

下記のような、workspace というディレクトリ内に新しい Rails アプリケーションを作る想定

└── workspace
    └── 新しいRailsアプリケーション

Gemfileの作成 (:warning: 初回のみ)

下記は、 workspace ディレクトリに初めてRailsアプリケーションを作成する際のみ必要な手順。
作ったことがある場合はやらなくてよい。

Gemfile 作成

workspace ディレクトリにGemfileを作成する

bundle init

Gemfile 更新

gem "rails" のコメントアウトを外す
(Railsの最新安定バージョンを入れたいので、バージョン指定はしていない。
特定のバージョンをインストールしたい場合は、バージョンを指定する)

Gemfile
 # frozen_string_literal: true

 source "https://rubygems.org"

- # gem "rails"
+ gem "rails"

バージョンを指定する場合の書き方の例:

gem "rails", "7.1.2"

Rubyバージョン設定

workspace ディレクトリで使うRubyのバージョンを設定する
(.tool-versionsが作成 or 更新される)

cd workspace
asdf local ruby 使いたいRubyのバージョン

bundle update 実行

Gemfile.lock が更新されて、gem がインストールされる
(初めて実行する際は、 Gemfile.lock が作成される)

bundle update

想定しているバージョンのRailsになっているかどうかは、Gemfile.lockの中身を確認する

例:

Gemfile.lock
    rails (7.1.2)

現在の workspace ディレクトリの状態

下記のような構成になっている

└── workspace
    ├── .ruby-version
    ├── Gemfile
    └── Gemfile.lock

Railsアプリケーションの作成

  • --skip-bundle: bundle install はしない
  • --skip-test: bundle install はしない
  • --database=データベースの種類: データベースの種類を指定
  • :star: その他のコマンドは bundle exec rails new -h で確認可能
bundle exec rails new sample --skip-bundle --skip-test --database=データベースの種類

rubyのバージョン設定

作成したアプリケーションで使うRubyのバージョンを設定する

cd sample
asdf local ruby 使いたいRubyのバージョン

RSpecの設定

最新は、公式のREADMEを参照する。以下は2023/11/27時点の内容。

  1. 下記をGemfileに追記
    Gemfile
    group :development, :test do
      gem 'rspec-rails', '~> 6.1.0'
    end
    
  2. bundle insall 実行
    bundle install
    
  3. generate コマンド実行
    bin/rails generate rspec:install
    

git の commit

git の user 設定

コミットログに残るユーザー名と名前の設定

git config --local user.name ユーザー名
git config --local user.email メールアドレス

Initial commit

ファイルの差分を確認して、問題なければ commit する

git add .
git commit -m "Initial commit"

(おまけ)RuboCopの設定

最新は、公式のREADMEを参照する。以下は2023/11/27時点の内容。

  1. 下記をGemfileに追記
    Gemfile
    group :development do
      gem 'rubocop-rails', require: false
      gem 'rubocop-rspec', require: false
    end
    
  2. bundle install 実行
  3. .rubocop.yml 作成
    .rubocop.yml
    require:
      - rubocop-rspec
      - rubocop-rails
    
    AllCops:
      NewCops: enable
      Exclude:
        - "bin/**/*"
        - "config/**/*"
        - "db/**/*"
        - "script/**/*"
        - "vendor/**/*"
        - "Rakefile"
        - "config.ru"
        - "Gemfile"
      TargetRubyVersion: 3.2
    
    Style/Documentation:
      Exclude:
        - !ruby/regexp /application_.*\.rb$/
    
  4. rubocop -A 実行

おわりに

脳死でできるようにしておくと、
新しいバージョンを試すときのハードルが下がるので楽。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2