Skip to content

start_project

irisAsh edited this page Jan 3, 2018 · 3 revisions

Rails プロジェクトのセットアップ

プロジェクトの作成

プロジェクト作成用に、任意のディレクトリに Bundle 環境を作成する。
ローカル環境の ruby のバージョンを指定する。

$ rbenv local 2.4.0

Gemfile の作成と Bundle 環境の初期化を行う。

$ rbenv exec gem install bundler
$ rbenv rehash
$ rbenv exec bundle init

ralis の Gem をインストールするために、 Gemfile を編集する。

Gemfile

source "https://rubygems.org"

gem “rails”, “5.1.3”

Rails をインストールする。

$ rbenv exec bundle install --path vendor/bundle

Rails プロジェクトを作成する。

$ rbenv exec bundle exec rails new learnrails --skip-bundle

プロジェクト作成用に作成した環境を削除する。

$ rm -f Gemfile
$ rm -f Gemfile.lock
$ rm -rf .bundle
$ rm -rf vender

Git 管理の編集

.gitignoreの最下行にvender/bundleを追加する。

プロジェクトの 初回起動

Gem をインストールする。

$ rbenv exec bundle install --path vendor/bundle

プロジェクトを起動する。

$ rbenv exec bundle exec rails server

DB 環境の設定 (Sqlite3 から Postgresql に変更する)

Gemfile を編集し、pgの Gem をインストールする。

Gemfile

# Use sqlite3 as the database for Active Record
#gem 'sqlite3'
# Use postgresql as the databese for Active Record
gem 'pg'

$ rbenv exec bundle install --path vendor/bundle

config/database.ymlを編集し、利用する DB を Postgresql に指定する。

config/database.yml

default: &default
adapter: postgresql
encoding: utf8
pool: 5
timeout: 5000

development:
  <<: *default
  database: learnrails_development

test:
  <<: *default
  database: learnrails_test

production:
  <<: *default
  database: learnrails_production
  username: learnrails
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>
  host: localhost
  port: 5432

DB を作成する。

$ bin/rake db:create
Created database 'learnrails_development'
Created database 'learnrails_test'

Postgresql から確認する。

$ psql -U postgres

postgres=# \l

Slim の導入

erubis, haml, hamlit, faml, slim とテンプレートエンジンは色々あるが、ここでは haml を利用することにする。 Gemfile に以下を追加。

gem "haml-rails"

Gem をインストール。

$ rbenv exec bundle install --path vendor/bundle

参考文献

Clone this wiki locally