A Rails Application Template for generating new Rails API labs with updated dependencies.
Usage:
rails new <lab-name> -BT --api --minimal -m=https://raw.githubusercontent.com/learn-co-curriculum/phase-4-rails-api-lab-template/main/template.rb
-m
uses the custom Flatiron lab template to provide some additional setup.-B
skips bundle install, since we'll want to make some changes before bundling.-T
skips test files, since we'll be usingrspec
.--api
configures the app with a limited set of middleware, and skips views/helpers/assets on resource generation.--minimal
excludes the following:action_cable
action_mailbox
action_mailer
action_text
active_job
active_storage
bootsnap
jbuilder
spring
system_tests
turbolinks
webpack
It's recommended to use Rails generators to add new code, since rspec
will
give you a template for models and controllers (requests) automatically when
you use rails g
.
In addition to rspec-rails
, these gems provide some additional matchers:
rspec-json_expectations
gives ainclude_json
matchershoulda-matchers
gives a bunch of model/controller matchers
The template.rb
file takes care of a few extra things, documented below:
Since we don't want to update the labs every time a new Ruby version is released, and want them to work for as many versions of Ruby as possible, we need to remove references to the Ruby version in the project:
- Delete the
.ruby-version
file - Remove the Ruby version at the top of the Gemfile
- Add
rspec-rails
gem to the development and test groups - Add
shoulda-matchers
gem to test group - Add
rspec-json_expectations
gem to test group - Run
rails generate rspec:install
To help with lessons on auth, this adds back in the session/cookie middleware.
- Update
config/application.rb
with middleware - Update
app/controllers/application_controller.rb
with cookie helpers
Optionally include a React client.
- Use
npx create-react-app client --use-npm
to generate a React app in theclient
directory - Add
"proxy": "http://localhost:3000"
topackage.json
to proxy API requests - Add
rake install
script
The foreman gem lets us run multiple
processes from a Procfile. This means we can create a rails start
script
to run Rails and React together.
- Add
Procfile.dev
with process runner instructions - Add
rake start
task
Note: foreman
shouldn't be included in the Gemfile. Instruct users to install
foreman
globally:
gem install foreman