Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
20
Help us understand the problem. What is going on with this article?
@yuki0627

Cloud9でPostgreSQLを利用しHerokuにRails5.xをデプロイした時の手順

More than 3 years have passed since last update.

HerokuにCloud9からRails5.0.0.1をデプロイする時に行った方法です。

ワークスペース作成

Cloud9に新規でワークスペースを作成
blankを選択
スクリーンショット 2016-10-08 15.31.39.png

Railsのインストール・アプリの作成

gem install rails --no-ri --no-rdoc
rails new myapp --database=postgresql
cd myapp
bundle install

Getting Started with Rails 5.x on Heroku

Hello World

rails generate controller welcome

app/views/welcome/index.html.erb

<h2>Hello World</h2>
<p>
  The time is now: <%= Time.now %>
</p>

config/routes.rb

root 'welcome#index'

この時点ではPostgreSQLの設定をしていないので rails s -p $PORT -b $IP をやってもエラーになります。

Cloud9でPostgreSQLを使えるようにする

PostgreSQL起動

sudo service postgresql start

ユーザー作成+α

sudo sudo -u postgres psql
postgres=# CREATE USER username SUPERUSER PASSWORD 'password';
postgres=# UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
postgres=# DROP DATABASE template1;
postgres=# CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';
postgres=# UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
postgres=# \c template1
postgres=# VACUUM FREEZE;
postgres=# \q

Cloud9の設定にユーザー名とパスワードを保存

echo "export USERNAME=username" >> ~/.profile
echo "export PASSWORD=password" >> ~/.profile
source ~/.profile

dababase.ymlを編集

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV['USERNAME'] %>
  password: <%= ENV['PASSWORD'] %>
  host:     <%= ENV['IP'] %>

development:
  <<: *default
  database: myapp_development

production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

Cloud9で動作確認

DB作成

rails db:create
rails db:migrate

起動

rails s -p $PORT -b $IP

Herokuにデプロイ

ログイン

heroku login

コミット

git init
git add .
git commit -m "init"

デプロイ

heroku create
git push heroku master

以上となります。
これでうまくいかない場合や不明点などありましたらお知らせ下さい。

20
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
yuki0627

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
20
Help us understand the problem. What is going on with this article?