Prerequisite
- Ruby: latest version at writing is 2.1.5
Getting Started
First ensure you're running the version of ruby you're expecting and not an ancient version like anything before Ruby 2.0.
$ ruby -v
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]
Install Rails 4.2.0
Next ensure that Rails 4.2.0 is installed:
$ gem install rails
Fetching: thread_safe-0.3.4.gem (100%)
Successfully installed thread_safe-0.3.4
[...]
Fetching: rails-4.2.0.gem (100%)
Successfully installed rails-4.2.0
25 gems installed
$ rails -v
Rails 4.2.0
Now we have access to the rails
command to generate a skeleton app. Use --skip-bundle
if you know you're going to want to edit the generated Gemfile before bundling. This will help you to avoid bundling twice. If we append && cd catbook
to the following command we'll automatically change directories after the first command completes:
$ rails new catbook --skip-bundle && cd catbook
Now we can edit the Gemfile
to our heart's content. Don't want jbuilder
? Deleted . Don't want turbolinks
? Gone.
Here's what my Gemfile
looks like for a toy application:
source "https://rubygems.org"
gem "rails", "~> 4.2.0"
gem "sqlite3"
# gem "sass-rails", "~> 5.0.0"
# gem "uglifier", ">= 1.3.0"
# gem "coffee-rails", "~> 4.1.0"
# gem "jquery-rails"
gem "turbolinks"
# gem "bcrypt", "~> 3.1.7"
group :production do
gem "puma"
end
group :development, :test do
gem "web-console", "~> 2.0.0"
gem "rspec-rails", "~> 3.1.0"
end
group :development do
gem "thin"
gem "byebug"
gem "guard-rspec", require: false
end
For starters let's use sqlite3
for our database, later I might switch to PostgreSQL with the pg
gem. I've commented out the asset pipeline as I don't generally begin with javascript assets but might want them later. I'm leaving bcrypt
commented out until I need it for an auth system. For production I'm a big fan of puma
and for development I prefer thin
because I've had issues with Webrick, even in development. For testings I'll start out with Rspec using the rspec-rails
gem and Guard with the guard-rspec
gem.
Now let's bundle!
$ bundle
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
Using rake 10.3.2
[...]
Installing web-console 2.0.0
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
Rspec
Let's make sure Rspec is ready:
$ rails generate rspec:install
create .rspec
create spec
create spec/spec_helper.rb
create spec/rails_helper.rb
$ rake
∴ rake
/Users/cat/.rubies/ruby-2.1.5/bin/ruby -I/Users/cat/.gem/ruby/2.1.5/gems/rspec-core-3.1.7/lib:/User
s/cat/.gem/ruby/2.1.5/gems/rspec-support-3.1.7/lib -S /Users/cat/.gem/ruby/2.1.5/gems/rspec-core-3.
1.7/exe/rspec
No examples found.
Guard
Great! Tests are ready to run. Let's make sure Guard is ready now too:
$ guard init rspec
The default .rspec
configuration dotfile has --warnings
enabled which is great when you're not using it with libraries that have a lot of warnings. With Rails projects, I remove this line.
Now we can boot up Guard and hit <return>
to manually trigger all tests.
$ bundle exec guard
17:24:56 - INFO - Guard is using Tmux to send notifications.
17:24:56 - INFO - Guard is using TerminalTitle to send notifications.
17:24:56 - INFO - Guard::RSpec is running
17:24:56 - INFO - Guard is now watching at '/Users/cat/catbook/'
[1] guard(main)>
17:24:57 - INFO - Run all
17:24:57 - INFO - Running all specs
No examples found.
Finished in 0.00038 seconds (files took 0.58517 seconds to load)
0 examples, 0 failures
Boot App
$ bundle exec rails s
=> Booting Thin
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on localhost:3000, CTRL+C to stop
Now we're up and running.
Edit: This post has been updated to reflect Ruby 2.1.5 and Rails 4.2 final
Ruby Forest Druid.
Cover image credit: https://flic.kr/p/g8LQAv