1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[memo] Rails5.1.5 アプリケーション新規作成・初期設定

Last updated at Posted at 2018-02-27

Rails newを使ったアプリケーションの新規作成と、簡単な初期設定の説明。
環境:AWS EC2 Ruby2.5.0 Rails5.1.5 MySQL5.7.21

Railsアプリケーション新規作成

$ rails new sample_app -d mysql -T --skip-bundle -s
引数 (参考) 意味
sample_app アプリケーション名
-d mysql DBにはMySQLを使用
-T test::unit をインストールしない
--skip-bundle 新規作成時に bundle install を行わない
-s 既存ファイルはそのまま残す

これ以降の作業はsample_appディレクトリ内で行う。

$ cd sample_app

タイムゾーン設定

config/application.rb
class Application < Rails::Application
  ...

  # Timezone
  config.time_zone = 'Tokyo'
  config.active_record.default_timezone = :local
end

Gemfile修正

Gemfile
...

# therubyracerのコメントアウトを外す
gem 'therubyracer', platforms: :ruby

...

# tzinfo-dataをコメントアウトする
# gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
```

### **bundle install実行**
```
$ bundle install --path vendor/bundle
```

### **vendor/bundleをgit管理から除外**
```ruby:.gitignore
/vendor/bundle
```

## **DBセットアップ**
```ruby:config/database.yaml
default: &default
  adapter: mysql2
  host: localhost
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000
  encoding: utf8mb4
  charset: utf8mb4
  collation: utf8mb4_general_ci
  username: mysql-user
  password: mysql-password
  socket: /socket/file/path/hoge.sock

...
```
| 項目 | 値 | 意味 |
|:--|:--|:--|
| adapter | mysql2 | DBはMySQL |
| host | localhost | 接続先はローカルサーバー |
| pool | <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> | コネクション数の上限値環境変数or固定値5 |
| timeout | 5000 | 接続タイムアウトミリ秒 |
| encoding | utf8mb4 | 文字コード  |
| charset | utf8mb4 | 文字コード |
| collation | utf8mb4_general_ci | 文字コード  |
| username | mysql-user | DB接続ユーザー  |
| password | mysql-password | DB接続ユーザーパスワード  |
| socket | /socket/file/path/hoge.sock | sockファイルの在処 |

### **DB作成**
```
$ bundle exec rake db:create
Created database 'sample_app_development'
Created database 'sample_app_development_test'
```
1
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?