Ruby on Rails project creation
Creating a new Rails 4 project.
- Check that the
.gitignore_global
file contains the files you want ignored. - Create the skeleton of a new Rails app
user@desktop:~$ mkdir project user@desktop:~$ cd project
- Import the
.gitignore
: For Rails, use this one from Github, below. Then initialise the repo:
user@desktop:~/project$ git init user@desktop:~/project$ git add . user@desktop:~/project$ rails new . --git --database=postgresql --skip-test-unit
- Modify the
Gemspec
file to refer to RSpec in the test evironment:
gem 'rails', '4.0.2' gem 'pg' gem 'sass-rails', '~> 4.0.0' gem 'uglifier', '>= 1.3.0' gem 'coffee-rails', '~> 4.0.0' gem 'jquery-rails' gem 'turbolinks' gem 'jbuilder', '~> 1.2' group :doc do gem 'sdoc', require: false end group :development do gem 'capistrano', '~> 3.0' gem 'capistrano-rails', '~> 1.1' gem 'capistrano-bundler','>= 1.1.0' gem 'capistrano-rvm' end group :development, :test do gem "rspec-rails" end group :test do gem "factory_girl_rails" gem "cucumber-rails", :require => false gem "capybara" gem "database_cleaner" gem "launchy" end
- then install the bundle and RSpec-Rails support:
user@desktop:~/project$ bundle install user@desktop:~/project$ rails generate rspec:install
- Create the database user for this project (development and test on the local machine)
user@desktop:~$ sudo -u postgres psql postgres=# create role projectdbuser with createdb login password 'projectdbpassword';
- Edit
config/database.yml
to include the passwords for the development and test databases. Addhost: localhost
lines for the development and test databases. - Create and migrate the database
user@desktop:~/project$ rake db:setup user@desktop:~/project$ rake db:migrate
- When running tests, migrate the test database (if necessary) and then run the tests:
user@desktop:~/project$ rake db:migrate RAILS_ENV=test user@desktop:~/project$ rake spec
Github's .gitignore file
*.rbc *.sassc .sass-cache capybara-*.html .rspec /log /tmp /db/*.sqlite3 /db/*.sqlite3-journal /public/system /coverage/ /spec/tmp **.orig rerun.txt pickle-email-*.html config/initializers/secret_token.rb config/secrets.yml ## Environment normalisation: /.bundle /vendor/bundle # these should all be checked in to normalise the environment: # Gemfile.lock, .ruby-version, .ruby-gemset # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: .rvmrc ## Github's global gitignore for Ruby *.gem *.rbc /.config /coverage/ /InstalledFiles /pkg/ /spec/reports/ /test/tmp/ /test/version_tmp/ /tmp/ ## Documentation cache and generated files: /.yardoc/ /_yardoc/ /doc/ /rdoc/ ## Environment normalisation: /.bundle/ /lib/bundler/man/ # for a library or gem, you might want to ignore these files since the code is # intended to run in multiple environments; otherwise, check them in: # Gemfile.lock # .ruby-version # .ruby-gemset # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: .rvmrc
See also
- Rails guides
- Setting up Ruby on Rails with Postgres
- Rspec-Rails
- Rails testing
- BetterSpecs (RSpec style guide)