These instructions will help you deploy your Finstagram app to Heroku (a web hosting service), so that the entire internet will be able to see and interact with your application! Before we get to deployment though, we need to make sure a couple of our files are setup properly.
If you've already deployed and want to continue making changes, see the Making Changes section near the bottom.
- In the terminal, type
git branch
- Take note of your current branch
- Type these commands:
git add .
git commit -m "pre-deployment"
git push
git checkout master
git merge NAME_OF_NOTED_BRANCH
- Continue on with Pre-Deployment: Create Seed Data
git add .
git commit -m "pre-deployment"
git push
- Create a
seeds.rb
file in thedb
folder - Copy the following into that file:
records = [
{
user: { username: "sharky_j", avatar_url: "https://live.staticflickr.com/65535/52358606250_01c667c5da_w.jpg", email: "sharky_j@gmail.com", password: "abcd" },
post: { photo_url: "https://live.staticflickr.com/65535/52358421508_786aa10e2c_c.jpg"}
},
{
user: { username: "kirk_whalum", avatar_url: "https://live.staticflickr.com/65535/52358421348_f34c7996b1.jpg", email: "kirk_w@gmail.com", password: "abcd" },
post: { photo_url: "https://live.staticflickr.com/65535/52357237337_1cc718f6a7_4k.jpg"}
},
{
user: { username: "marlin_peppa", avatar_url: "https://live.staticflickr.com/65535/52358415933_0a0e6bc35f_3k.jpg", email: "marlin_p@gmail.com", password: "abcd" },
post: { photo_url: "https://live.staticflickr.com/65535/52358494794_f88b160d15_4k.jpg"}
}
]
puts "=== Seeding database... ==="
# create Users and FinstagramPosts
records.each do |record|
# Create a User
user = User.create record[:user]
puts "-- Created User: #{user.username}"
# Create a FinstagramPost
finstagram_post = record[:post]
finstagram_post[:user_id] = user.id
FinstagramPost.create finstagram_post
puts "-- Created a FinstagramPost for User: #{user.username}"
end
puts "=== Seeding complete. ==="
Ensure your Gemfile
file contains the following code (should match exactly).
Pay attention to: new group :production
and the pry
gem is no longer in a group
source "https://rubygems.org"
gem 'rake'
gem 'activesupport'
gem 'activerecord', '< 6.1.0'
gem 'bcrypt'
gem 'sinatra', '~> 2.2.2'
gem 'sinatra-contrib'
gem 'sinatra-activerecord'
gem 'puma'
gem 'tux'
gem 'pry'
group :development, :test do
gem 'shotgun'
gem 'sqlite3', '~> 1.5.3'
end
group :production do
gem 'pg'
gem 'rails_12factor'
end
After saving the Gemfile
changes,
- Delete your
Gemfile.lock
file - In a terminal type
bundle install
Ensure your config/database.rb
file contains the following code (should match exactly).
Important things that are different: if/else
block for different database definition
configure do
# Log queries to STDOUT in development
if Sinatra::Application.development?
ActiveRecord::Base.logger = Logger.new(STDOUT)
set :database, {
adapter: "sqlite3",
database: "db/db.sqlite3"
}
else
db = URI.parse(ENV['DATABASE_URL'])
set :database, {
adapter: "postgresql",
host: db.host,
username: db.user,
password: db.password,
database: db.path[1..-1],
encoding: "utf8"
}
end
# Load all models from app/models, using autoload instead of require
# See http://www.rubyinside.com/ruby-techniques-revealed-autoload-1652.html
Dir[APP_ROOT.join('app', 'models', '*.rb')].each do |model_file|
filename = File.basename(model_file).gsub('.rb', '')
autoload ActiveSupport::Inflector.camelize(filename), model_file
end
end
Ensure your db/migrate/0_create_base_tables.rb
file contains the following code (should match exactly).
Only difference is the first line. Specifically the brackets with a version number
class CreateBaseTables < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
t.string :username
t.string :avatar_url
t.string :email
t.string :password
t.timestamps
end
create_table :finstagram_posts do |t|
t.references :user
t.string :photo_url
t.timestamps
end
create_table :comments do |t|
t.references :user
t.references :finstagram_post
t.text :text
t.timestamps
end
create_table :likes do |t|
t.references :user
t.references :finstagram_post
t.timestamps
end
end
end
If you implemented proper password hashing, you'll have to add versioning [5.2]
to your other migration file as well.
In a terminal type the following and press enter:
npm i -g heroku
1. Create an account at heroku.com.
- Make sure you remember your login email and password!
- Heroku will ask you to confirm your email (which you have to do before your account is active/usable).
2. Go back to Gitpod, and in a terminal tab, type the following and hit enter:
heroku login -i
- Enter your login credentials
- If it worked, it will say something like
Logged in as you@youremail.com
- Next, still in your terminal tab, type this and press enter:
heroku create
- Copy the URL that it spits out- this will be how you'll access your application once it's deployed! The URL should look like
https://stark-reef-21398.herokuapp.com/
- If you want to rename your app, enter:
heroku apps:rename [new-app-name]
3. Still in your terminal tab
- Run the command
heroku addons:create heroku-postgresql:hobby-dev
- If you receive
Couldn't find that app.
(Otherwise go to step 4)- Type:
heroku git:remote -a YOUR-APP-NAME
(The app name is the two random words followed by some numbers in the URL). Press enter.
- Type:
4. Then commit your code:
git status
git add .
git commit -m "Readies app for deployment!"
5. In the terminal tab, launch your app by typing: git push heroku
6. Then, run the db migration with this command: heroku run bundle exec rake db:migrate
7. Followed by, seeding data into the database with the following: heroku run bundle exec rake db:seed
8. Lastly, push your changes to Github: git push origin master
Remember the URL you wrote down after you typed heroku create
? Yep, now is the time to visit it in your browser! If you did everything correctly, it should display your Finstagram app! :)
Once your app is live, you can still make changes to your code and add new features! You can make your changes in Gitpod as you would normally, then once you're done, make sure all your changes are saved.
If you're starting up your gitpod workspace again, you'll need to do a couple initial steps to connect heroku first:
- In the terminal, type:
npm i -g heroku
- After it's installed, and still in the terminal, type:
heroku login -i
- Log in to Heroku with your credentials
- Now you're ready to continue working on your app!
After you've made some changes and tested them locally to ensure your app is working, follow the next steps to push your changes to heroku.
1. Open a terminal tab and commit your changes as you normally would: E.g.
git status
git add name_of_file.rb
git commit -m "describes your changes"
2. Still in the terminal tab, push your changes
- To Heroku:
git push heroku
- To GitHub:
git push origin master
3. Now go to your Heroku app, refresh the page, and your changes will be live! π€
When developing your app on Gitpod, you were able to do bundle exec tux
to edit things in your database and just generally play around with ruby code. Once your app is deployed to Heroku, using tux won't change things on the Heroku version of your app, it'll only change them on Gitpod.
To make changes to data in the database of your Heroku app, you should go into Gitpod, open a new terminal
tab, and then run this command:
heroku run bundle exec tux
(and then you can use tux as you normally would)