備忘録 ToyBox Rails3.0
Rails3.0の事始めとして、なにかひとつのモノを作るではなく
実験用プロジェクトを創りたいと思います。
(小さなひとつのモノを作ったほうがいいんでしょうけど)
環境
ruby 1.9.2
gem 1.3.7
git 1.6.5.2
参考資料
WEB+DB PRESS vol.58 (Rails3特集のやつ)
Rails 3.0のインストール
gem install rails -v 3.0.0
=> 22個くらいのgemが入った。
*** LOCAL GEMS *** abstract (1.0.0) actionmailer (3.0.0) actionpack (3.0.0) activemodel (3.0.0) activerecord (3.0.0) activeresource (3.0.0) activesupport (3.0.0) arel (1.0.1) builder (2.1.2) bundler (1.0.0) erubis (2.6.6) i18n (0.4.1) mail (2.2.6.1) mime-types (1.16) polyglot (0.3.1) rack (1.2.1) rack-mount (0.6.13) rack-test (0.5.6) rails (3.0.0) railties (3.0.0) rake (0.8.7) thor (0.14.2) treetop (1.4.8) tzinfo (0.3.23)
heroku
heroku用のgemを入れる
gem install heroku
Rails アプリケーションの作成
prototype.jsとTest::Unitはいらないためオプションを付けて実行
# rails new toy_box -J -T create create README create Rakefile create config.ru create .gitignore create Gemfile create app create app/controllers/application_controller.rb create app/helpers/application_helper.rb create app/views/layouts/application.html.erb create app/mailers create app/models create config create config/routes.rb create config/application.rb create config/environment.rb create config/environments create config/environments/development.rb create config/environments/production.rb create config/environments/test.rb create config/initializers create config/initializers/backtrace_silencers.rb create config/initializers/inflections.rb create config/initializers/mime_types.rb create config/initializers/secret_token.rb create config/initializers/session_store.rb create config/locales create config/locales/en.yml create config/boot.rb create config/database.yml create db create db/seeds.rb create doc create doc/README_FOR_APP create lib create lib/tasks create lib/tasks/.gitkeep create log create log/server.log create log/production.log create log/development.log create log/test.log create public create public/404.html create public/422.html create public/500.html create public/favicon.ico create public/index.html create public/robots.txt create public/images create public/images/rails.png create public/stylesheets create public/stylesheets/.gitkeep create public/javascripts create public/javascripts/.gitkeep create public/javascripts/application.js create script create script/rails create tmp create tmp/sessions create tmp/sockets create tmp/cache create tmp/pids create vendor/plugins create vendor/plugins/.gitkeep
gitで管理する
アプリケーションのルートディレクトリに移り以下を実行
git init
git add .
git commit -m "new app"
herokuにコミット
heroku create toy_box
Name must start with a letter and can only contain letters, numbers, and dashes 怒られた。 アンダーバーがダメらしいのでmv mv toy_box toy-box heroku create toy-box Creating toy-box..... done Created http://toy-box.heroku.com/ | git@heroku.com:toy-box.git 成功。
https://heroku.com/myapps
にアクセスして作ったのができているか確認
git pushでherokuへ!
# git push git@heroku.com:toy-box.git master Counting objects: 52, done. Delta compression using up to 2 threads. Compressing objects: 100% (38/38), done. Writing objects: 100% (52/52), 22.10 KiB, done. Total 52 (delta 2), reused 0 (delta 0) -----> Heroku receiving push -----> Rails app detected -----> Detected Rails is not set to serve static_assets Installing rails3_serve_static_assets... done -----> Gemfile detected, running Bundler version 1.0.0 Unresolved dependencies detected; Installing... ! Gemfile.lock will soon be required ! Check Gemfile.lock into git with `git add Gemfile.lock` ! See http://docs.heroku.com/bundler Fetching source index for http://rubygems.org/ Installing rake (0.8.7) Installing abstract (1.0.0) Installing activesupport (3.0.0) Installing builder (2.1.2) Installing i18n (0.4.1) Installing activemodel (3.0.0) Installing erubis (2.6.6) Installing rack (1.2.1) Installing rack-mount (0.6.13) Installing rack-test (0.5.6) Installing tzinfo (0.3.23) Installing actionpack (3.0.0) Installing mime-types (1.16) Installing polyglot (0.3.1) Installing treetop (1.4.8) Installing mail (2.2.6.1) Installing actionmailer (3.0.0) Installing arel (1.0.1) Installing activerecord (3.0.0) Installing activeresource (3.0.0) Using bundler (1.0.0) Installing thor (0.14.2) Installing railties (3.0.0) Installing rails (3.0.0) Installing sqlite3-ruby (1.3.1) with native extensions Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed. Your bundle was installed to `.bundle/gems` Compiled slug size is 3.6MB -----> Launching.... done http://toy-box.heroku.com deployed to Heroku To git@heroku.com:toy-box.git * [new branch] master -> master
あとは勝手にデプロイしてくれます。
毎回git@なんちゃらと打つのは疲れるので登録しましょう
git remote add heroku git@heroku.com:toy-box.git
ところでherokuのRubyのバージョンは?
heroku stack
で知ることができます。
aspen-mri-1.8.6 * bamboo-ree-1.8.7 bamboo-mri-1.9.1 (beta) bamboo-mri-1.9.2
こんな感じの結果。
今は1.8.7で動いているようですね。
今回のアプリケーションは1.9.2で作るのでRubyの変更をしましょう。
# heroku stack:migrate bamboo-mri-1.9.2 -----> Preparing to migrate toy-box bamboo-ree-1.8.7 -> bamboo-mri-1.9.2 NOTE: You must specify ALL gems (including Rails) in manifest Please read the migration guide: http://docs.heroku.com/bamboo -----> Migration prepared. Run 'git push heroku master' to execute migration.
トップページを作る
Welcomeページを作る。
WelcomeControllerのindexメソッドをルートにしようと思います。
rails g controller welcome
※gはgenerate 短くていい!
※ちまたではaliasを貼って r = rails としている人が多いみたい
※r g controller welcome
config/routes.rbに
root :to => "welcome#index"
と書く…
Rails2系だと
map.root :controller => "welcome", :action => "index"
と書いてたのが
だいぶ簡単に!
とはいえ構文がかなり違うので最初は戸惑いそうです。
コントローラーとビューも適当に作成してコミット…
する前に
public/index.htmlを削除する。
忘れ物がないかを確認してコミット
heroku open
で自分のサイトが開く。
便利。