How to build a Ruby-On-Rails and React app from scratch
- Create a repo in Github
-
Install last version of ruby:
rbenv install 3.0.0 rbenv global 3.0.0
-
Install last version of node.js: nvm is equivalent to rbenv: allow to manage several versions of node.js on the same computer. Versions are set on a per-project basis.
nvm install node nvm alias default node nvm use node --version
-
Install rails
gem install rails
-
Bootstrap a new app.
rails new my-app \ --database=postgresql \ --api \
-
Set the NodeJS version in our new app.
cd my-app echo "15.8.0" > .nvmrc
-
Let’s check the used ruby version.
cat .ruby-version
-
Push to Github
git remote add origin git@github.com:<USER>/<REPO>.git git commit -m "first commit"
-
Test run the app
bundle exec rails s
-
I want to use PostgreSQL and avoid manually installing the database by using Docker-Compose. Create and configure Docker:
version: "3.7" services: database: image: "postgres" # use latest official postgres version ports: - "127.0.0.1:5432:5432" environment: POSTGRES_USER: ${USER} POSTGRES_HOST_AUTH_METHOD: "trust" volumes: - ${PWD}/db:${PWD}/db # persist data even if container shuts downvolumes: healthcheck: test: ["CMD", "pg_isready", "-U", "${USER}"] container_name: quiz_postgres
-
Install postgresql and disable it.
sudo apt install postgresql sudo systemctl disable postgresql sudo systemctl stop postgresql
-
Launch docker and connect to the DB:
docker-compose up -d psql -h 127.0.0.1 -U <USER> # Connect to the DB directly docker-compose run database bash # Or connect inside the docker container... psql -h database -U <USER> # ...and then to the DB
-
Next time you want to start or stop docker, don’t do
up
but instead do:docker-compose stop docker-compose start
- Edit database.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
url: <%= "postgres://#{ENV['USER']}:@localhost:5432/quiz-development" %>
test:
<<: *default
url: <%= "postgres://#{ENV['USER']}:@localhost:5432/quiz-test" %>
-
Start rails and create a db:
bundle exec rails db:create bundle exec rails db:migrate bundle exec rails s