Rails新規アプリケーションの作り方


## GitHubでリポジトリつくる
![Screen Shot 2020-11-20 at 21.55.27.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/174291/8839eb18-c4a1-3ac3-c2c8-c021ea972b61.png)

## localにcloneしてくる

```
$ git clone https://github.com/k-tetsuhiro/rails_docker_sample_3.git
& cd rails_docker_sample_3
```

## bundleを初期化する
```
$ bundle init
Writing new Gemfile to /Users/kakuno/sandbox/rails_docker_sample_3/Gemfile
```

## Gemfileができているので編集する

```
$ vi Gemfile
```

```file:Gemfile
# frozen_string_literal: true
source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem "rails"
```
→ # gem "rails" のコメントアウトをはずす

## bundle installして必要なものをインストールする

```
$ bundle install --path vendor/bundle
[DEPRECATED] The `--path` flag is deprecated because it relies on being remembered across bundler invocations, which bundler will no longer do in future versions. Instead please use `bundle config set path 'vendor/bundle'`, and stop using this flag
Fetching gem metadata from https://rubygems.org/.............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
--- 必要なgemがはいっていく... ---

```

## Railsアプリケーションを作成する

```
$ bundle exec rails new . -B -d mysql --skip-turbolinks --skip-test
      exist
      create  README.md
      create  Rakefile
      create  .ruby-version
      create  config.ru
      create  .gitignore
    conflict  Gemfile
Overwrite /Users/kakuno/sandbox/rails_docker_sample_3/Gemfile? (enter "h" for help) [Ynaqdhm]
---　ここで上書きしていいか聞かれるのでYを押して許可する ---
```
→最後になにかエラーメッセージが出てたらもう一度bundle installする


```
$ bundle install --path vendor/bundle
```

## webpackerをinstallする

```
$ rails webpacker:install
      create  config/webpacker.yml
Copying webpack core config
      create  config/webpack
      create  config/webpack/development.js
--- 必要なものが入っていく ---
```

## Railsサーバーを起動してみる

```
$ bundle exec rails s
```
→ようやくRails起動！！ `http://localhost:3000/`　でアクセスできる（ただしまだMySQLなどの設定をしてないのでエラー画面になるはず）


## gitで管理しないものを設定する
```
$ vi .gitignore
```
→無視するファイルを設定

```file:.gitignore
# Created by https://www.toptal.com/developers/gitignore/api/rails
# Edit at https://www.toptal.com/developers/gitignore?templates=rails

### Rails ###
*.rbc
capybara-*.html
.rspec
/db/*.sqlite3
/db/*.sqlite3-journal
/db/*.sqlite3-[0-9]*
/public/system
/coverage/
/spec/tmp
*.orig
rerun.txt
pickle-email-*.html

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

# TODO Comment out this rule if you are OK with secrets being uploaded to the repo
config/initializers/secret_token.rb
config/master.key

# Only include if you have production secrets in this file, which is no longer a Rails default
# config/secrets.yml

# dotenv, dotenv-rails
# TODO Comment out these rules if environment variables can be committed
.env
.env.*

## Environment normalization:
/.bundle
/vendor/bundle

# these should all be checked in to normalize the environment:
# Gemfile.lock, .ruby-version, .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

# if using bower-rails ignore default bower_components path bower.json files
/vendor/assets/bower_components
*.bowerrc
bower.json

# Ignore pow environment settings
.powenv

# Ignore Byebug command history file.
.byebug_history

# Ignore node_modules
node_modules/

# Ignore precompiled javascript packs
/public/packs
/public/packs-test
/public/assets

# Ignore yarn files
/yarn-error.log
yarn-debug.log*
.yarn-integrity

# Ignore uploaded files in development
/storage/*
!/storage/.keep

# End of https://www.toptal.com/developers/gitignore/api/rails
```

## コミットとプッシュ
```
$ git add *
$ git commit -m "first commit"
$ git push
```
→これでGitHubに更新される

![Screen Shot 2020-11-20 at 22.24.27.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/174291/5024a685-e4c9-211c-00ab-8dada8b21fb0.png)



## 番外編

### rbenvを最新にする
https://qiita.com/_kanacan_/items/c1499f6c13b1c41da982

### railsを最新にする
https://techacademy.jp/magazine/22193

[参考]
https://qiita.com/yuitnnn/items/b45bba658d86eabdbb26