Heroku Deployment of Rails App - estermer/ericstermer.com GitHub Wiki

Heroku

  1. git init, git add -A, git commit -m "First Commit"
  2. $ heroku create
  3. $ git push heroku master
  4. $ heroku run rails db:migrate

Add data through the Heroku Rails Console

  1. $ heroku run console

    patient = Patient.create(name: "Schmitty", insurance_co: "Anthem", gender: "M", new_patient: true)

doctor = Doctor.create(name: "Dr. Robert", specialty: "feet", insurance: true, gender: "M") appointment_one = Appointment.create(location: "Grady General", day: "Monday", reason: "smelly feet", doctor_id: 1, patient_id: 1)

doctor.patients.create(name: "Diesel", insurance_co: "Blue Cross", gender: "M", new_patient: false) patient.doctors.create(name: "Dr. Foster", specialty: "Veterinary", gender: "M") appointment_two = Appointment.create(location: "Inman Park", day: "Friday", reason: "shots", doctor_id: doctor.id, patient_id: patient.id)


#### We could also add some data to our `db/seeds.rb` using the Faker gem

1. Add to `db/seeds.rb`

 ```rb
Appointment.destroy_all
Patient.destroy_all
Doctor.destroy_all

 10.times do
     Patient.create(name: Faker::Name.name, insurance_co: Faker::Beer.malts, gender: "F", new_patient: Faker::Boolean.boolean)
     Doctor.create(name: Faker::Name.name, specialty: Faker::Hipster.word, gender: "F", insurance: Faker::Boolean.boolean)
 end

 10.times do
     Appointment.create(location: Faker::University.name, day: Faker::Date.forward(23), reason: Faker::Hipster.sentence(6), patient_id: Faker::Number.between(1, 10), doctor_id: Faker::Number.between(1, 10))
 end
  1. rails db:reset

    Note: This will drop the local databases, recreate, migrate and seed

  2. Make sure to do another git add -A and git commit to push the file up to Heroku so we can seed our production database.

  3. $ heroku pg:reset DATABASE

  4. $ heroku run rails db:migrate

  5. Then run $ heroku run rails db:seed

Test out the API with Postman

  1. <NAME_ OF_YOUR_HEROKU_APP_HERE>/api/patients

  2. <NAME_ OF_YOUR_HEROKU_APP_HERE>/api/doctors

  3. Try to add a new Patient.

  4. Try to add an Appointment.

Angular

  1. mkdir rails5-api-angular-frontend_2 && cd into it

  2. npm init -y

  3. touch server.js

    var express = require('express');
    var app     = express();
    var path    = require('path');
    
    app.use(express.static(path.join(__dirname,'public')));
    
    app.get('/', function(req, res){
        res.render('index');
    });
    
    app.listen(4000, function(){
        console.log("app listening on port 4000");
    });
  4. npm install --save express path

  5. mkdir public

  6. mkdir public/js

  7. touch public/js/app.js

    (function(){
        angular.module('Rails5', []);
    })()

1. `touch public/js/rails5Controller.js`

    ```js
    (function(){
      angular.module('Rails5')
        .controller('rails5Controller', rails5Controller);
    
    
      function rails5Controller($http){
    
        var self = this;        
        var server = "<YOUR_HEROKU_API_LINK_HERE>"
        // For example, var server = 'https://enigmatic-garden-65625.herokuapp.com/api/'
    
        $http.get(`${server}/doctors`)
          .then(function(response) {
              self.doctors = response.data;
              console.log(self.doctors[0].name);
        });
      }
    })()
  1. touch public/index.html

    <!DOCTYPE html>
    <html ng-app="Rails5">
      <head>
        <meta charset="utf-8">
        <title>Rails 5 API App</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
        <script src='js/app.js'></script>
        <script src='js/rails5Controller.js'></script>
      </head>
      <body ng-controller="rails5Controller as rails">
        <h1>Rails 5</h1>
        <ul ng-repeat="appointment in rails.appointments">
          <li>{{appointment.location}}</li>
    
        </ul>
        {{1 + 1}}
      </body>
    </html>
  2. touch Procfile - web: node server.js

⚠️ **GitHub.com Fallback** ⚠️