-
Notifications
You must be signed in to change notification settings - Fork 9
Rails3
This quickstart will get you going with Rails 3.x on the Cedar stack. For Sinatra or other Ruby apps, please see the Ruby quickstart.
このクイックスタートは、Cedar stack上の Rails 3.x より開始します。 Rails 3.1ガイドは、こちらです。http://devcenter.heroku.com/articles/rails31_heroku_cedar Sinatraまたは、その他Rubyアプリは、Ruby quickstartを参照して下さい。
-
Basic Ruby/Rails knowledge, including an installed version of Ruby 1.9.2, Rubygems, Bundler, and Rails 3.
-
Ruby/Railsの基礎知識、Ruby 1.9.2, Rubygems, Bundler, Rails 3がインストール済みであること。
-
Basic Git knowledge
-
Gitの基礎知識
-
Your application must run on Ruby (/wiki/MRI) 1.9.2.
-
アプリケーションは、 Ruby (/wiki/MRI) 1.9.2上で実装する必要があります。
Install the Heroku Toolbelt on your local workstation. This ensures that you have access to the Heroku command-line client, Foreman, and the Git revision control system.
Heroku Toolbeltをローカル端末へインストールして下さい。 インストールすることで、Heroku command-line client、 Foreman、Gitのリビジョン管理システムへのアクセスが保証されます。
Once installed, you'll have access to the heroku
command from your command shell. Log in using the email address and password you used when creating your Heroku account:
一度、インストールを行うと、シェルからheroku
コマンドを使用することが出来るようになります。
Herokuアカウント作成時のemailアドレスとパスワードを使用してログインして下さい。(以下、実行例)
:::term
$ heroku login
Enter your Heroku credentials.
Email: adam@example.com
Password:
Could not find an existing public key.
Would you like to generate one? [Yn]
Generating new SSH public key.
Uploading ssh public key /Users/adam/.ssh/id_rsa.pub
Press enter at the prompt to upload your existing ssh
key or create a new one, used for pushing code later on.
コマンドプロンプト画面にて、既存のsshキーをアップロードする、または、新規sshキーの作成を行いますので、 enterキーを押して下さい。sshキーは、コードをHerokuへpushする際に使用されます。
You may be starting from an existing app. If not, a vanilla Rails 3 app will serve as a suitable sample app: 既存のアプリから始めることになるでしょうが、既存のアプリが存在しない場合、標準的なRails 3アプリがサンプルアプリとして 適切でしょう。(以下、作成例)
:::term
$ rails new myapp
$ cd myapp
開発期間中は、PostgreSQLを使用することを強く推奨します。開発環境とディプロイ環境の同期を保つことで、 環境依存による微少な違いから発生するバグを見つけることの煩わしさから解放されます。
Since Heroku provides you a PostgreSQL database for your app, edit your Gemfile
and change this line:
Herokuは、PostgreSQLのデータベースを提供するため、Gemfile
の以下の箇所を編集して下さい。
(追記:Rails3のデフォルトデータベースは、SQLiteのため)
:::ruby
gem 'sqlite3'
To this: 上記内容をこちらに変更
:::ruby
gem 'pg'
And re-install your dependencies (to generate a new Gemfile.lock
):
依存関係を再インストールします。(新たにGemfile.lock
を作成するため)
:::term
$ bundle install
$ git init
$ git add .
$ git commit -m "init"
Create the app on the Cedar stack: Cedar stack上へアプリの作成:
:::term
$ heroku create --stack cedar
Creating severe-mountain-793... done, stack is cedar
http://severe-mountain-793.herokuapp.com/ | git@heroku.com:severe-mountain-793.git
Git remote heroku added
Deploy your code: コードをディプロイ:
:::term
$ git push heroku master
Counting objects: 67, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (52/52), done.
Writing objects: 100% (67/67), 86.33 KiB, done.
Total 67 (delta 5), reused 0 (delta 0)
-----> Heroku receiving push
-----> Rails app detected
-----> Installing dependencies using Bundler version 1.1
Checking for unresolved dependencies.
Unresolved dependencies detected.
Running: bundle install --without development:test --path vendor/bundle --deployment
Fetching source index for http://rubygems.org/
Installing rake (0.8.7)
...
Installing rails (3.0.5)
Your bundle is complete! It was installed into ./vendor/bundle
-----> Rails plugin injection
Injecting rails_log_stdout
Injecting rails3_serve_static_assets
-----> Discovering process types
Procfile declares types -> (none)
Default types for Rails -> console, rake, web, worker
-----> Compiled slug size is 8.3MB
-----> Launching... done, v5
http://severe-mountain-793.herokuapp.com deployed to Heroku
To git@heroku.com:severe-mountain-793.git
* [new branch] master -> master
Before looking at the app on the web, let's check the state of the app's processes:
:::term
$ heroku ps
Process State Command
------------ ------------------ ------------------------------
web.1 up for 5s bundle exec rails server -p $PORT
The web process is up. Review the logs for more information:
:::term
$ heroku logs
2011-03-10T11:10:34-08:00 heroku[web.1]: State changed from created to starting
2011-03-10T11:10:37-08:00 heroku[web.1]: Running process with command: `bundle exec rails server -p 53136`
2011-03-10T11:10:40-08:00 app[web.1]: [2011-03-10 19:10:40] INFO WEBrick 1.3.1
2011-03-10T11:10:40-08:00 app[web.1]: [2011-03-10 19:10:40] INFO ruby 1.9.2 (2010-12-25) [x86_64-linux]
2011-03-10T11:10:40-08:00 app[web.1]: [2011-03-10 19:10:40] INFO WEBrick::HTTPServer#start: pid=12198 port=53136
2011-03-10T11:10:42-08:00 heroku[web.1]: State changed from starting to up
Looks good. We can now visit the app with heroku open
.
Cedar allows you to launch a Rails console process attached to your local terminal for experimenting in your app's environment:
:::term
$ heroku run console
Running `bundle exec rails console` attached to terminal... up, ps.1
Loading production environment (Rails 3.0.4)
irb(main):001:0>
Rake can be run as an attached process exactly like the console:
:::term
$ heroku run rake db:migrate
By default, your app's web process runs rails server
, which uses Webrick. This is fine for testing, but for production apps you'll want to switch to a more robust webserver. We recommend Thin.
To use Thin with Rails 3, add it to your Gemfile
:
:::ruby
gem 'thin'
Run bundle install
to set up your bundle locally.
Change the command used to launch your web process by creating a Procfile, like this:
web: bundle exec rails server thin -p $PORT -e $RACK_ENV
Set the RACK_ENV
to development in your environment
$ echo "RACK_ENV=development" >>.env
Test your Procfile locally using Foreman:
$ foreman start
11:35:11 web.1 | started with pid 3007
11:35:14 web.1 | => Booting thin
11:35:14 web.1 | => Rails 3.0.4 application starting in development on http://0.0.0.0:5000
11:35:14 web.1 | => Call with -d to detach
11:35:14 web.1 | => Ctrl-C to shutdown server
11:35:15 web.1 | >> Thin web server (v1.2.8 codename Black Keys)
11:35:15 web.1 | >> Maximum connections set to 1024
11:35:15 web.1 | >> Listening on 0.0.0.0:5000, CTRL+C to stop
Looks good, so press Ctrl-C to exit. Deploy your changes to Heroku:
$ git add .
$ git commit -m "use thin via procfile"
$ git push heroku
Check ps
, you'll see the web process uses your new command specifying Thin as the webserver:
:::term
$ heroku ps
Process State Command
------------ ------------------ ------------------------------
web.1 starting for 3s bundle exec rails server thin -p $..
The logs also reflect that we are now using Thin:
:::term
$ heroku logs
2011-03-10T11:38:43-08:00 heroku[web.1]: State changed from created to starting
2011-03-10T11:38:47-08:00 heroku[web.1]: Running process with command: `bundle exec rails server thin -p 34533`
2011-03-10T11:38:50-08:00 app[web.1]: => Booting Thin
2011-03-10T11:38:50-08:00 app[web.1]: => Rails 3.0.4 application starting in production on http://0.0.0.0:34533
2011-03-10T11:38:50-08:00 app[web.1]: => Call with -d to detach
2011-03-10T11:38:50-08:00 app[web.1]: => Ctrl-C to shutdown server
2011-03-10T11:38:50-08:00 app[web.1]: >> Thin web server (v1.2.7 codename No Hup)
2011-03-10T11:38:50-08:00 app[web.1]: >> Maximum connections set to 1024
2011-03-10T11:38:50-08:00 app[web.1]: >> Listening on 0.0.0.0:34533, CTRL+C to stop
2011-03-10T11:38:55-08:00 heroku[web.1]: State changed from starting to up
There are several options for invoking the Rails 3.1+ asset pipeline when deploying to Heroku. For full details please see the Rails 3.1+ Asset Pipeline on Heroku Cedar article.
If you push up your app and it crashes (heroku ps
shows state crashed
), check your logs to find out what went wrong. Here are some common problems.
If your app failed to require a sourcefile, chances are good you're running Ruby 1.9.1 or 1.8 in your local environment. The load paths have changed in Ruby 1.9. Port your app forward to Ruby 1.9.2 making certain it works locally before trying to push to Cedar again.
Ruby 1.9 added more sophisticated encoding support to the language. Not all gems work with Ruby 1.9 (see isitruby19 for information on a particular gem). If you hit an encoding error, you probably haven't fully tested your app with Ruby 1.9.2 in your local environment. Port your app forward to Ruby 1.9.2 making certain it works locally before trying to push to Cedar again.
If your app crashes due to missing a gem, you may have it installed locally but not specified in your Gemfile
. You must isolate all local testing using bundle exec
. For example, don't run ruby web.rb
, run bundle exec ruby web.rb
. Don't run rake db:migrate
, run bundle exec rake db:migrate
.
Another approach is to create a blank RVM gemset to be absolutely sure you're not touching any system-installed gems:
:::term
$ rvm gemset create myapp
$ rvm gemset use myapp
If you're still missing a gem when you deploy, check your Bundler groups. Heroku builds your app without the development
or test
groups, and if you app depends on a gem from one of these groups to run, you should move it out of the group.
One common example using the RSpec tasks in your Rakefile
. If you see this in your Heroku deploy:
:::term
$ heroku run rake -T
Running `bundle exec rake -T` attached to terminal... up, ps.3
rake aborted!
no such file to load -- rspec/core/rake_task
Then you've hit this problem. First, duplicate the problem locally like so:
:::term
$ bundle install --without development:test
...
$ bundle exec rake -T
rake aborted!
no such file to load -- rspec/core/rake_task
Now you can fix it by making these Rake tasks conditional on the gem load. For example:
:::ruby
begin
require "rspec/core/rake_task"
desc "Run all examples"
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w[--color]
t.pattern = 'spec/*_spec.rb'
end
rescue LoadError
end
Confirm it works locally, then push to Heroku.