I Deployed My Sinatra App To Heroku

MY SINATRA PORTFOLIO PROJECT IS DONE!!!

You can say I’m pretty stoked. This blog is part of the requirements though so get ready :-). Actually, I didn’t take enough notes to really get into the code in this blog. I did hit some solid stumbling blocks and there is some major refactoring of code needing to happen but I’m overall pretty happy. A large part of the time I spent was making the app look good utilizing Bootstrap.

Something that definitely needs to be refactored is one of my helper methods:

def current_user_parents
  parents = []
  current_user(session).houses.each do |house|
    Parent.where(house_id: house.id).each do |parent|
      parents << parent
    end
  end
  parents
end

It’s sandwich code (starts with a variable set to an empty array, fills that array, returns that array) so I know I can most likely eliminate two lines from that or even three if I’m not pushing onto that parents variable anymore. We’ll see. I’m interested to pick the brain of the instructor that goes over the project with me.

You can see what I’ll call v0.8 (I figure it’s close to v1.0 but not yet lol) at https://your-neighbors.herokuapp.com/. I feel like the code is solid but still some functionality and visual things that need to be resolved.

  • I’d like to add a static footer to the site.
  • If a house is deleted the parent and children remain in the database but can never be accessed again because of how I list them in relation to the house they’re in.
  • When there are no parents or children I need to add a link suggesting to create some.
  • When a user tries to access the info for something they didn’t create (ie. they created house 8 but try and go to the link for house 5) the error page tells them to log in even though they are logged in. I need to create a different error page for logged in users trying to access stuff that isn’t there’s vs. the error page when a person isn’t logged in.
  • Add some color!

I’m sure I could find more. Feel free to check out the app. Sign up, use it, and report issues to the GitHub link at the top right on every page of the application.

It was interesting deploying to Heroku as well. There isn’t a solid guide on deploying a Sinatra app with a DB to Heroku. Through Heroku’s docs and this Get Started with Sinatra on Heroku from 2013, I got it up and running though. The biggest thing was setting up Postgres and what to put in the Procfile. Let me see if I can remember what exactly I did:

  1. Add gem 'pg' to your Gemfile. Also, define your version of ruby, ie. ruby '2.4.1' in your Gemfile.
  2. I changed the sqlite gem to development like this gem 'sqlite3', :group => :development.
  3. I added a config/database.yml because the Heroku docs said to but I don’t know if it’s needed. I’ll probably delete it and see what happens in the near future. The Heroku docs have an example that I used and just changed the database: names.
  4. I think config/environment.rb is where the magic happened. I kept the :development environment using sqlite3 because I read in some of the docs that you needed to know the host, username, password, port and something else to make it work locally. I figured if I could get it to work without figuring that out I would do that. I got this working locally running shotgun and on the Heroku server too. With the changes it now looks like this:
    require 'bundler/setup'
    Bundler.require
    
    configure :development do
      ENV['SINATRA_ENV'] ||= "development"
    
      ActiveRecord::Base.establish_connection(
        :adapter => "sqlite3",
        :database => "db/neighborhood#{ENV['SINATRA_ENV']}.sqlite"
      )
    end
    
    configure :production do
      db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')
    
      ActiveRecord::Base.establish_connection(
        :adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
        :host     => db.host,
        :username => db.user,
        :password => db.password,
        :database => db.path[1..-1],
        :encoding => 'utf8'
      )
    end
    
    require_all 'app'
    
  5. The Procfile was something I couldn’t figure out. The SitePoint article I linked above had this line for it and this is what made it all work web: bundle exec rackup config.ru -p $PORT.
  6. For the rest I followed the Ruby docs on Heroku here and the SQLite doc here.
  7. If you get db migration errors you can run rake tasks on Heroku. heroku run rake db:migrate was one that I needed to do. More in depth docs about it here.

Hey, look at that. I remembered way more than I thought. Hopefully, this was helpful to someone somewhere. Probably a student coming behind me who is going to pull their hair out trying to get their app to work on Heroku.

Time spent today: 8:32
Time spent total: 198:13
Lessons completed today: 1
Lessons completed total: 406