LoginSignup
59
67

More than 5 years have passed since last update.

Rails+Herokuで5分でWebアプリ作るおっ( ^ω^)

Last updated at Posted at 2015-02-18

Ruby on RailsとはRubyのWebアプリケーションフレームワーク。
Web界ではもっとも有名なフレームワークと言っても過言ではないのではないでしょうか。

HerokuはPaaSの一つで、当初はRailsのために作られたものなのでRailsとの相性は抜群です。

今日はこれを使って5分で、Memberを登録しておくだけの簡単なWebアプリを作りますよ。環境はMacです。

個々の詳細については特に説明しておりません。最短の手順を記述するのみです。

インストールしてあるもの

  • gitをインストール済み
  • rubyをインストール済み
  • Postgresインストール済み
  • gem install rails済み
  • heroku toolbeltインストール済み

タイムアタック前の確認事項

heroku login

すぐログイン切れちゃうのでheroku appsなんかを打って確認しておくしておく。
パスワード入力と2way authentication codeが必要。

PostgreSQL

別タブでpostgresを起動しておく。
Macでやっているのでdaemon化はしていないのです。

$ postgres -D /usr/local/var/postgres

準備ができたらタイムアタック開始!

Railsアプリを作る

Memberを登録するだけのアプリを作ります。

$ rails new memberlist -d postgresql

時間がかかります。

$ cd memberlist
$ rails s 

http://localhost:3000/ をブラウザで叩いて表示されることを確認。まだDBの設定をしていないのでエラーになります。

次にScaffoldで/membersページをつくり、MigrationでDBを作ります。

$ createdb memberlist_development
$ rails g scaffold Member name:string comment:text
$ rake db:migrate
$ rails s

再びブラウザから確認。これでアプリが完成しました。

Gitリポジトリ作る

$ git init
$ git add .
$ git commit -m'initial commit'

Herokuへデプロイ

GemfileとProcfileというファイルが必要です。

Gemfileは既にできています。
Heroku対応させるために以下を末尾に追加。

Gemfile
gem 'rails_12factor', group: :production

反映。

$ bundle install

Procfileを作ります。

$ vim Procfile
# 中身は以下のようにする。
web: bundle exec rails server -p $PORT

ここいらでcommitしましょう。

$ git commit -a -m'Create procfile'
$ heroku create

Heroku上で設定がされ、またリポジトリにherokuというリモートリポジトリが作成されます。

$ git remote -v
heroku  https://git.heroku.com/nantoka-kantoka-4491.git (fetch)
heroku  https://git.heroku.com/nantoka-kantoka-4491.git (push)

デプロイ。

$ git push heroku master

少し時間かかる。
これでブラウザで動作ができるはず

Heroku上のDB設定

HerokuでのPostgreSQLの設定。アドオンを追加して、migrationします。

$ heroku addons:add heroku-postgresql
$ heroku run rake db:migrate

これで出力されたURLで見られる。

アプリについての情報は以下で見られる。

$ heroku apps:info

参考資料

ドットインストールをかなり参考にさせていただきました。

59
67
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
59
67