-
Notifications
You must be signed in to change notification settings - Fork 9
rails4 getting started
Ruby on Rails is a popular web framework written in Ruby. The beta for Rails 4.0 was released on February 25, 2013. For running previous versions of Rails on Heroku see Getting Started with Rails 3.x on Heroku.
Ruby on Railsは、Rubyで書かれたポピュラーなフレームワークです。Rails 4.0のベータ版が、2013年1月25日にリリースされました。以前のバージョンのRailsをHeroku上でお使いになる場合は、Getting Started with Rails 3.x on Herokuを参照して下さい。
This article provides an in-depth getting started guide for Rails 4. If you are already familiar with Heroku and Rails, reference the simplifed Rails 4 on Heroku guide instead.
この記事は、Rails 4を使い始めるに辺り、深く掘り下げた記事となります。既に、HerokuとRailsに慣れ親しんでいる場合は、simplifed Rails 4 on Heroku guideを参照して下さい。
To run Rails 4 on Heroku you will need:
Rails 4をHeroku上で動作させるには、以下のものが必要となります。:
-
Basic Ruby/Rails knowledge, including an installed version of Ruby 1.9.3+, Rubygems, Bundler, and Rails 4.
-
基本的なRuby/Railsの知識。Ruby 1.9.3以降、Rubygems、Bundler、Rails 4がインストール済みであること。
-
Basic Git knowledge
-
基本的なGitの知識
-
A Heroku user account. Signup is free and instant.
-
Herokuのユーザーアカウント。サインアップは、フリー、かつ簡単に行えます。
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. You will also need Ruby and Rails installed.
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:
Note that $
before commands indicates they should be run on the command line, prompt, or terminal with appropriate permissions.
:::term
$ heroku login
Enter your Heroku credentials.
Email: [email protected]
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.
You may be starting from an existing app, if so upgrade to Rails 4 before continuing. If not, a vanilla Rails 4 app will serve as a suitable sample app. To build a new app make sure that you're using the Rails 4.x using $ rails -v
. You can get the new version of rails by running,
:::term
$ gem install rails --version 4.0.0
Then create a new app:
:::term
$ rails new myapp --database=postgresql
$ cd myapp
Heroku integration has previously relied on using the Rails plugin system, which has been removed from Rails 4. To enable features such as static asset serving and logging on Heroku please add the following gems to your Gemfile
:
:::ruby
gem 'rails_12factor', group: :production
Then run:
:::term
$ bundle install
That should be the minimum you need to do to integrate with Heroku.
If you did not specify postgresql
while creating your app (using --database=postgresql
) you will need to add the pg
gem to your Rails project. Edit your Gemfile
and change this line:
:::ruby
gem 'sqlite3'
To this:
:::ruby
gem 'pg'
And re-install your dependencies (to generate a new Gemfile.lock
):
:::term
$ bundle install
You can get more information on why this change is needed and how to configure your app to run postgres locally see why you cannot use Sqlite3 on Heroku.
Rails 4 requires Ruby 1.9.3 or above. Heroku has a recent version of Ruby installed, however you can specify an exact version by putting this in your Gemfile
add:
:::ruby
ruby "1.9.3"
or
:::ruby
ruby "2.0.0"
You should also be running the same version of Ruby locally. You can verify by running $ ruby -v
. You can get more information on specifying your Ruby version on Heroku here.
Heroku relies on git, a distributed source control managment tool, for deploying your project. If your project is not already in git first verify that git
is on your system:
:::term
$ git --help
usage: git [--version] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
[-c name=value] [--help]
<command> [<args>]
# ...
If you don't see any output or get command not found
you will need to install it on your system, verify that the Heroku toolbelt is installed.
Once you've verified that git works, first make sure you are in your Rails app directory. Now run these three commands in your Rails app directory to initialize and commit your code to git:
:::term
$ git init
$ git add .
$ git commit -m "init"
Now that your application is committed to git you can deploy to Heroku.
Make sure you are in the directory that contains your Rails app, then create the app on Heroku:
:::term
$ heroku create
Creating calm-brook-1268... done, stack is cedar
http://calm-brook-1268.herokuapp.com/ | [email protected]:calm-brook-1268.git
Git remote heroku added
Verify that the remote was added to your project by running
:::term
$ git config -e
If you do not see fatal: not in a git directory
then you are safe to deploy to Heroku. After you deploy you will need to migrate your database, make sure it is properly scaled and use logs to debug any issues that come up.
Deploy your code:
:::term
$ git push heroku master
Counting objects: 112, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (77/77), done.
Writing objects: 100% (112/112), 27.01 KiB, done.
Total 112 (delta 20), reused 112 (delta 20)
-----> Ruby/Rails app detected
-----> Using Ruby version: ruby-2.0.0
-----> Installing dependencies using Bundler version 1.3.0.pre.5
Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment
Fetching gem metadata from https://rubygems.org/.........
Fetching gem metadata from https://rubygems.org/..
Fetching git://github.com/heroku/rails3_serve_static_assets.git
Fetching git://github.com/heroku/rails_log_stdout.git
Installing rake (10.0.3)
Installing i18n (0.6.2)
Installing minitest (4.6.1)
Installing multi_json (1.6.1)
Installing atomic (1.0.1)
Installing thread_safe (0.1.0)
Installing tzinfo (0.3.35)
Installing activesupport (4.0.0)
Installing builder (3.1.4)
Installing erubis (2.7.0)
Installing rack (1.5.2)
Installing rack-test (0.6.2)
Installing actionpack (4.0.0)
Installing mime-types (1.21)
Installing polyglot (0.3.3)
Installing treetop (1.4.12)
Installing mail (2.5.3)
Installing actionmailer (4.0.0)
Installing activemodel (4.0.0)
Installing activerecord-deprecated_finders (0.0.3)
Installing arel (4.0.0)
Installing activerecord (4.0.0)
Installing coffee-script-source (1.5.0)
Installing execjs (1.4.0)
Installing coffee-script (2.2.0)
Using json (1.7.7)
Installing rdoc (3.12.2)
Installing thor (0.17.0)
Installing railties (4.0.0)
Installing coffee-rails (4.0.0)
Installing hike (1.2.1)
Installing jbuilder (1.0.2)
Installing jquery-rails (2.2.1)
Installing pg (0.14.1)
Using bundler (1.3.0)
Installing tilt (1.3.3)
Installing sprockets (2.9.0)
Installing sprockets-rails (2.0.0.rc3)
Installing rails (4.0.0)
Using rails3_serve_static_assets (0.0.1) from git://github.com/heroku/rails3_serve_static_assets.git (at master)
Using rails_log_stdout (0.0.1) from git://github.com/heroku/rails_log_stdout.git (at master)
Installing sass (3.2.6)
Installing sass-rails (4.0.0)
Installing turbolinks (1.0.0)
Installing uglifier (1.3.0)
Your bundle is complete! It was installed into ./vendor/bundle
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:
<= 1.8.6 : unsupported
= 1.8.7 : gem install rdoc-data; rdoc-data --install
= 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!
Cleaning up the bundler cache.
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
I, [2013-02-25T15:15:26.999810 #1771] INFO -- : Writing /tmp/build_8twtg5uo0zrj/public/assets/rails-a48208150b2c0da4f80797a999919b58.png
I, [2013-02-25T15:15:29.993217 #1771] INFO -- : Writing /tmp/build_8twtg5uo0zrj/public/assets/application-e4bf17ac068b4157db532671a5294743.js
I, [2013-02-25T15:15:30.065780 #1771] INFO -- : Writing /tmp/build_8twtg5uo0zrj/public/assets/application-a543268ce31a2798b68675fbfcb1bcdc.css
Asset precompilation completed (6.24s)
-----> Rails plugin injection
-----> Discovering process types
Procfile declares types -> (none)
Default types for Ruby/Rails -> console, rake, web, worker
-----> Compiled slug size: 33.1MB
-----> Launching... done, v6
http://calm-brook-1268.herokuapp.com deployed to Heroku
To [email protected]:calm-brook-1268.git
* [new branch] master -> master
Note: that Rails 4 no longer has a static index page in production, if you're using a new app, there may be no root page.
If you are using the database in your application you need to manually migrate the database by running:
:::term
$ heroku run rake db:migrate
Any commands after the heroku run
will be executed on a Heroku dyno.
You've deployed your code to Heroku. You can now instruct Heroku to execute a process type. Heroku does this by running the associated command in a dyno - a lightweight container which is the basic unit of composition on Heroku.
Let's ensure we have one dyno running the web
process type:
:::term
$ heroku ps:scale web=1
You can check the state of the app's dynos. The heroku ps
command lists the running dynos of your application:
:::term
$ heroku ps
=== web: `rails server -p $PORT -e $RAILS_ENV`
web.1: up for 5s
Here, one dyno is running.
We can now visit the app in our browser with heroku open
.
:::term
$ heroku open
Opening calm-brook-1268... done
Heroku gives you a default web app name for simplicty while you are developing. When you are ready to scale up and use Heroku for production you can add your own Custom Domain.
If you run into any problems getting your app to perform properly, you will first need to check the logs.
Heroku treats logs as streams of time-ordered events aggregated from the output streams of all the dynos running the components of your application. Heroku’s Logplex provides a single channel for all of these events.
You can view information about your running app using one of the logging commands, heroku logs
:
:::term
$ heroku logs
2013-02-26T01:47:32+00:00 heroku[web.1]: State changed from created to starting
2013-02-26T01:47:33+00:00 heroku[web.1]: Starting process with command `rails server -p 16142 -e $RAILS_ENV`
2013-02-26T01:47:35+00:00 app[web.1]: [2013-02-26 01:47:35] INFO WEBrick 1.3.1
2013-02-26T01:47:35+00:00 app[web.1]: [2013-02-26 01:47:35] INFO WEBrick::HTTPServer#start: pid=2 port=16142
2013-02-26T01:47:35+00:00 app[web.1]: [2013-02-26 01:47:35] INFO ruby 2.0.0 (2013-02-24) [x86_64-linux]
2013-02-26T01:47:36+00:00 heroku[web.1]: State changed from starting to up
You can also get the full stream of logs by running the logs command with the --tail
flag like this:
:::term
$ heroku logs --tail
Having only a single web dyno running will result in the dyno going to sleep after one hour of inactivity. This causes a delay of a few seconds for the first request upon waking. Subsequent requests will perform normally.
To avoid this, you can scale to more than one web dyno. For example:
:::term
$ heroku ps:scale web=2
For each application, Heroku provides 750 free dyno-hours. Running your app at 2 dynos would exceed this free, monthly allowance, so let's scale back:
:::term
$ heroku ps:scale web=1
Heroku allows you to run commands in a one-off dyno - scripts and applications that only need to be executed when needed - using the heroku run
command. Use this to launch a Rails console process attached to your local terminal for experimenting in your app's environment:
:::term
$ heroku run rails console
Running `rails console` attached to terminal... up, run.2591
Loading production environment (Rails 4.0.0)
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. On Cedar, we recommend Unicorn as the webserver. Regardless of the webserver you choose, production apps should always specify the webserver explicitly in the Procfile
.
First, add Unicorn to your application Gemfile
:
gem 'unicorn'
Run $ bundle install
, now you are ready to configure your app to use Unicorn.
Create a configuration file for Unicorn at config/unicorn.rb
:
$ touch config/unicorn.rb
Now we're going to add Unicorn specific configuration options, that we explain in detail in Heroku's Unicorn documentation:
# config/unicorn.rb
worker_processes 3
timeout 30
preload_app true
before_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT'
end
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
This default configuration assumes a standard Rails app with Active Record. You should get acquainted with the different options in the official Unicorn documentation.
Finally you will need to tell Heroku how to run your Rails app by creating a Procfile
in the root of your application directory.
Change the command used to launch your web process by creating a file called Procfile and entering this:
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
Note: The case of Procfile
is sensitive, the first letter must be uppercase.
Test your Procfile locally using Foreman:
:::term
$ foreman start
18:24:56 web.1 | I, [2013-03-13T18:24:56.885046 #18793] INFO -- : listening on addr=0.0.0.0:5000 fd=7
18:24:56 web.1 | I, [2013-03-13T18:24:56.885140 #18793] INFO -- : worker=0 spawning...
18:24:56 web.1 | I, [2013-03-13T18:24:56.885680 #18793] INFO -- : master process ready
18:24:56 web.1 | I, [2013-03-13T18:24:56.886145 #18795] INFO -- : worker=0 spawned pid=18795
18:24:56 web.1 | I, [2013-03-13T18:24:56.886272 #18795] INFO -- : Refreshing Gem list
18:24:57 web.1 | I, [2013-03-13T18:24:57.647574 #18795] INFO -- : worker=0 ready
Looks good, so press Ctrl-C to exit. Deploy your changes to Heroku:
:::term
$ git add .
$ git commit -m "use unicorn via procfile"
$ git push heroku
Check ps
, you'll see the web process uses your new command specifying Unicorn as the web server
:::term
$ heroku ps
Process State Command
------------ ------------------ ------------------------------
web.1 starting for 3s unicorn -p $..
The logs also reflect that we are now using Unicorn:
:::term
$ heroku logs
2013-03-14T01:57:55+00:00 heroku[web.1]: State changed from up to starting
2013-03-14T01:57:59+00:00 heroku[web.1]: Starting process with command `unicorn -p 26253 -E $RACK_ENV`
2013-03-14T01:58:00+00:00 app[web.1]: => Rails 4.0.0 application starting in production on http://0.0.0.0:27993
2013-03-14T01:58:00+00:00 app[web.1]: I, [2013-03-14T01:58:00.253906 #2] INFO -- : listening on addr=0.0.0.0:26253 fd=7
2013-03-14T01:58:00+00:00 app[web.1]: I, [2013-03-14T01:58:00.254256 #2] INFO -- : worker=0 spawning...
2013-03-14T01:58:00+00:00 app[web.1]: I, [2013-03-14T01:58:00.257455 #2] INFO -- : master process ready
2013-03-14T01:58:00+00:00 app[web.1]: I, [2013-03-14T01:58:00.258541 #4] INFO -- : Refreshing Gem list
2013-03-14T01:58:00+00:00 app[web.1]: I, [2013-03-14T01:58:00.258293 #4] INFO -- : worker=0 spawned pid=4
2013-03-14T01:58:01+00:00 app[web.1]: I, [2013-03-14T01:58:01.347337 #4] INFO -- : worker=0 ready
2013-03-14T01:58:01+00:00 heroku[web.1]: State changed from starting to up
There are several options for invoking the Rails asset pipeline when deploying to Heroku. For full details please see the Rails 3.1+ Asset Pipeline on Heroku Cedar article.
The config.assets.initialize_on_precompile
option has been removed and not needed for Rails 4. Also, any failure in asset compilation will now cause the push to fail.
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 you're 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:
:::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.