naichi's lab

3日後の自分(他人)への書き置き

【Rails4】rails newからherokuでの実行まで

f:id:naichilab:20160712004409p:plain >>> f:id:naichilab:20160712004417p:plain

Railsアプリの新規作成からherokuでの実行までを自分用にメモ

Rails5はまだよく分かってないのでとりあえずRails4で。

環境

  • Mac OS X El Capitan 10.11.5
  • Rails 4.2.2
  • Ruby(rbenv) 2.2.2

手順

Railsプロジェクトの作成まで

フォルダの作成と初期化

$ mkdir ~/sample-app
$ cd ~/sample-app
$ bundle init

Gemfileを修正

# A sample Gemfile
source "https://rubygems.org"
gem 'rails',        '4.2.2'

railsのgemをインストール

$ bundle install --path vendor/bundle

railsプロジェクトを同フォルダに上書き作成

$ bundle exec rails new .

動作確認

$ bundle exec rails s

下記にアクセスし、Railsの画面が出ていればOK

http://localhost:3000/

f:id:naichilab:20160712004524p:plain:w620

GitHubへプッシュ

git初期化

$ git init

.gitignoreを修正

# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
#   git config --global core.excludesfile '~/.gitignore_global'

# Ignore bundler config.
/.bundle

# Ignore bundler gems.
vendor/bundle

# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal

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

# coverage files.
coverage/*

# Mac finder artifacts
.DS_Store

# RubyMine project files
.idea
.idea/*

# dotenv environment file
.env

Gitのコミット

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

GitHubでリポジトリを作っておき、プッシュする

(SourceTree使用したのでコマンド割愛)

herokuへ

herokuのためにGemfile書き換え

herokuはpostgreSQL使うみたい。

group :production do
  gem 'pg'
  gem 'rails_12factor'
end

group :development, :test do
  gem 'sqlite3'
end

bundle install

$ bundle install --without production

(初回のみ)ブラウザでherokuへログインし、heroku toolbelt をインストール

toolbelt.heroku.com

バージョン確認

$ heroku version
heroku-cli: Installing CLI... 20.15MB/20.15MB
heroku-toolbelt/3.43.5 (x86_64-darwin10.8.0) ruby/1.9.3
heroku-cli/5.2.24-4b7e305 (darwin-amd64) go1.6.2
You have no installed plugins.

ターミナルからherokuへログイン

ターミナルで
$ heroku login
Enter your Heroku credentials.
Email: naichilab@live.jp
Password (typing will be hidden):
Logged in as naichilab@live.jp

herokuアプリの作成

$ heroku create
Heroku CLI submits usage information back to Heroku. If you would like to disable this, set `skip_analytics: true` in /Users/naichi/.heroku/config.json
Creating app... done, ⬢ quiet-wildwood-36613
https://quiet-wildwood-36613.herokuapp.com/ | https://git.heroku.com/quiet-wildwood-36613.git

ダッシュボードにアプリが作成されている https://dashboard.heroku.com/apps

f:id:naichilab:20160712004908p:plain

herokuへデプロイ

$ git push heroku master

これでデプロイ完了?あっけない

ダッシュボードからアプリを開くとそれっぽく出ている

f:id:naichilab:20160712005002p:plain

db:migrate

$ heroku run rake db:migrate
Running rake db:migrate on ⬢ quiet-wildwood-36613... up, run.8064
   (8.8ms)  CREATE TABLE "schema_migrations" ("version" character varying NOT NULL)
   (3.8ms)  CREATE UNIQUE INDEX  "unique_schema_migrations" ON "schema_migrations"  ("version")

ブラウザで見てみる

$ heroku open

エラーしてる。

f:id:naichilab:20160712005052p:plain

ログを確認する

$ heroku logs
もしくは
$ heroku logs --tail

2016-07-11T15:31:00.543888+00:00 app[web.1]: Started GET "/" for 211.135.251.123 at 2016-07-11 15:31:00 +0000
2016-07-11T15:31:00.546637+00:00 app[web.1]:
2016-07-11T15:31:00.546653+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/"):
2016-07-11T15:31:00.546654+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
2016-07-11T15:31:00.546656+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
2016-07-11T15:31:00.546657+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/rack/logger.rb:38:in `call_app'
2016-07-11T15:31:00.546657+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/rack/logger.rb:20:in `block in call'
2016-07-11T15:31:00.546676+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/tagged_logging.rb:68:in `block in tagged'
2016-07-11T15:31:00.546676+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/tagged_logging.rb:26:in `tagged'
2016-07-11T15:31:00.546677+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/tagged_logging.rb:68:in `tagged'
2016-07-11T15:31:00.546678+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/rack/logger.rb:20:in `call'
2016-07-11T15:31:00.546678+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/request_id.rb:21:in `call'
2016-07-11T15:31:00.546679+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
2016-07-11T15:31:00.546680+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
2016-07-11T15:31:00.546681+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
2016-07-11T15:31:00.546682+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/static.rb:113:in `call'
2016-07-11T15:31:00.546682+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
2016-07-11T15:31:00.546683+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/engine.rb:518:in `call'
2016-07-11T15:31:00.546684+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/application.rb:164:in `call'
2016-07-11T15:31:00.546684+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/lock.rb:17:in `call'
2016-07-11T15:31:00.546685+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/content_length.rb:15:in `call'
2016-07-11T15:31:00.546685+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/handler/webrick.rb:88:in `service'
2016-07-11T15:31:00.546686+00:00 app[web.1]:   vendor/ruby-2.2.4/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
2016-07-11T15:31:00.546687+00:00 app[web.1]:   vendor/ruby-2.2.4/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
2016-07-11T15:31:00.546687+00:00 app[web.1]:   vendor/ruby-2.2.4/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

RoutingErrorしてる。

ググるとconfig/routes.rbにrootを設定しろって話だけど まだコントローラーも何も作ってないので省略。

たぶん動くでしょう。

とりあえずここまで。

あとがき

この手順だとGitHub不要ですね。

herokuの管理画面みるとConnect to GitHubってボタンがあるしGitHubをリポジトリとして使うこともできそう。

おいおい触っていこう。