Rails init - izudon/izudon.github.io GitHub Wiki

1.Rails の取得

  • bundler は別途 apt 等でインストールしておく。

Gemfile の生成

$ bundle init

Gemfile の編集

$ vi Gemfile

gem の格納先をローカルに設定

$ bundle config set --local path 'vendor/bundle'

gem のインストール(第一段階)

$ bundle install
  • 依存関係にある gem が続々とインストールされてゆく。
$ ls vendor/bundle/ruby/3.0.0/bin/
nokogiri  racc  rackup  rails  rake  thor
  • rails コマンドは確かにインストールされている。
$ bundle exec rails --version
Rails 7.0.4.3
  • bundle exec 経由で叩ける。バージョンを確認できる。

2.Rails プロジェクトの作成

$ bundle exec rails new --api .
  • 上記はAPIモードの Rails プロジェクトを作成するコマンド。
$ bundle exec rails new --api .
       exist  
      create  README.md
      create  Rakefile
      create  .ruby-version
      create  config.ru
      create  .gitignore
      create  .gitattributes
    conflict  Gemfile
Overwrite /home/izui/study/renga/server/Gemfile? (enter "h" for help) [Ynaqdhm]
Y
       force  Gemfile
         run  git init from "."
  • Gemfile を上書きするかどうか聞かれるので Y と答える。
  • Gemfile が上書きされて、Rails の Gemfile になる。
  • モデルやコントローラなどの初期ファイルが次々と生成される。

Rails のテスト起動

$ rails s -p 4001 -b 0.0.0.0
=> Booting Puma
=> Rails 7.0.4.3 application starting in development 
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.6.5 (ruby 3.0.2-p107) ("Birdie's Version")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 2358
* Listening on http://0.0.0.0:4001
Use Ctrl-C to stop
  • ブラウザで確認すると、静的なページだが表示される。

3.バージョン管理

gems をバージョン管理外にする

$ vi .gitignore
  • こういった行を追加する。
    # Ignore gems
    /vendor/bundle/
    

リポジトリに追加・確認・コミット

$ git add -A .
$ git status
$ git commit -m "first commit"

リモートリポジトリの追加

$ git remote add origin ssh://...
$ git push --set-upstream origin main
$ git push