A template for starting projects with rails-api
. Includes authentication.
At the beginning of each cohort, update the versions in Gemfile
.
Install with bundle install
.
- Download this template.
- Unzip and rename the template directory (
unzip ~/Downloads/rails-api-template-master.zip
) - Move into the new project and
git init
.
- Empty
README.md
and fill with your own content. - Rename your app module in
config/application.rb
(changeRailsApiTemplate
). - Rename your project database in
config/database.yml
(change'rails-api-template'
).
- Install dependencies with
bundle install
. git add
andgit commit
your changes.
- bin/rails db:drop (if it already exists)
- bin/rails db:create
- bin/rails db:migrate
- bin/rails db:seed
- bin/rails db:examples
Note: Remember to follow the same commands when setting up your deployed database!
- Run the API server with
bin/rails server
orbundle exec rails server
.
This template follows the standard project structure in Rails.
User authentication is built-in.
Developers should run these often!
bin/rails routes
lists the endpoints available in your API.bin/rails console
opens a REPL that pre-loads the API.bin/rails db
opens your database client and loads the correct database.bin/rails server
starts the API.
Use this as the basis for your own API documentation. Add a new third-level heading for your custom entities, and follow the pattern provided for the built-in user authentication documentation.
Verb | URI Pattern | Controller#Action |
---|---|---|
POST | /sign-up |
users#signup |
POST | /sign-in |
users#signin |
PATCH | /change-password |
users#changepw |
DELETE | /sign-out |
users#signout |
Request:
curl http://localhost:8000/sign-up \
--include \
--request POST \
--header "Content-Type: application/json" \
--data '{
"credentials": {
"email": "'"${EMAIL}"'",
"password": "'"${PASSWORD}"'",
"password_confirmation": "'"${PASSWORD}"'"
}
}'
[email protected] PASSWORD=hannah curl-scripts/auth/sign-up.sh
Response:
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
{
"user": {
"id": 1,
"email": "[email protected]"
}
}
Request:
curl http://localhost:8000/sign-in \
--include \
--request POST \
--header "Content-Type: application/json" \
--data '{
"credentials": {
"email": "'"${EMAIL}"'",
"password": "'"${PASSWORD}"'"
}
}'
[email protected] PASSWORD=hannah curl-scripts/auth/sign-in.sh
Response:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
"user": {
"id": 1,
"email": "[email protected]",
"token": "BAhJIiVlZDIwZTMzMzQzODg5NTBmYjZlNjRlZDZlNzYxYzU2ZAY6BkVG--7e7f77f974edcf5e4887b56918f34cd9fe293b9f"
}
}
Request:
curl --include --request PATCH "http://localhost:8000/change-password" \
--header "Authorization: Token token=$TOKEN" \
--header "Content-Type: application/json" \
--data '{
"passwords": {
"old": "'"${OLDPW}"'",
"new": "'"${NEWPW}"'"
}
}'
OLDPW='hannah' NEWPW='elle' TOKEN='BAhJIiVlZDIwZTMzMzQzODg5NTBmYjZlNjRlZDZlNzYxYzU2ZAY6BkVG--7e7f77f974edcf5e4887b56918f34cd9fe293b9f' sh curl-scripts/auth/change-password.sh
Response:
HTTP/1.1 204 No Content
Request:
curl http://localhost:8000/sign-out \
--include \
--request DELETE \
--header "Authorization: Token token=$TOKEN"
TOKEN='BAhJIiVlZDIwZTMzMzQzODg5NTBmYjZlNjRlZDZlNzYxYzU2ZAY6BkVG--7e7f77f974edcf5e4887b56918f34cd9fe293b9f' sh curl-scripts/auth/sign-out.sh
Response:
HTTP/1.1 204 No Content
This is not a task developers should run often, but it is sometimes necessary.
locally
bin/rails db:migrate VERSION=0
bin/rails db:migrate db:seed db:examples
heroku
heroku run rails db:migrate VERSION=0
heroku run rails db:migrate db:seed db:examples
- rails-heroku-setup-guide
- http://guides.rubyonrails.org/api_app.html
- https://blog.codeship.com/building-a-json-api-with-rails-5/
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].