Hosting Rails app on Heroku is pretty straightforward - minus the preasset compiling issues you get with using Bootstrap and etc. However, recently I began revisiting my Sinatra apps. I haven't put any of them up on Heroku but now I want to because I'm interested in creating smaller web apps that don't need to a heavy Rails platform.
General Structure of Sinatra Application Files:
The structure should look something like this:
- / app /
- / views /
- firstLandingPage.erb
- secondLandingPage.erb
(THIS IS WHERE YOUR ERB[Embeded Ruby]/HTML FILES LIVE )
- / public /
- pubilcFilesHere.png
(THIS IS WHERE YOUR PICTURES AND OTHER PUBLIC DOCS LIVE)
- pubilcFilesHere.png
- app.rb
(THIS IS YOUR ROUTES AND CONTROLLER ROLLED UP IN ONE) - Gemfile
(THIS IS WHERE YOU PUT YOUR RUBY GEMS)
(BY HAVING THIS FILE, IT "NOTIFIES/TELLS" HEROKU THAT THIS APP USES RUBY) - config.ru
(IF YOU WANT TO DEPLOY YOUR SINATRA APP TO HEROKU YOU NEED THIS)
(CHECK OUT: http://www.sinatrarb.com/intro.html#When%20to%20use%20a%20config.ru?) - Procfile
(IF YOU WANT TO DEPLOY YOUR SINATRA APP TO HEROKU YOU NEED THIS TOO)
- / views /
Step 1. Gem Install Sinatra
In Your Command Line
>> gem install sinatra
Step 2. Create Your Sinatra App!
In your root directory "MyCoolSinatraApp": >> cd MyCoolSinatraApp >> mkdir views >> mkdir public >> touch app.rb >> touch config.ru >> touch Gemfile >> touch Procfile
Go into your "views/" and "public" folders and create and add files: >> cd views >> touch firstLandingPage.erb >> touch secondLandingPage.erb >> cd .. >> cd public >> etc...
Edit Your Routes and Controller "app.rb": require 'sinatra' get '/' do erb :firstLandingPage end
get '/secondLandingPage' do erb :secondLandingPage
end
Edit Your ERB/HTML FILES "views/
firstLandingPage.erb
": <h1>First Landing Page</h1> <a href="secondLandingPage">Second Landing Page</a>
Edit Your ERB/HTML FILES "views/
firstLandingPage.erb
": <h1>Second Landing Page</h1>
Run You App: >> ruby app.rb [2014-02-24 13:29:36] INFO WEBrick 1.3.1 [2014-02-24 13:29:36] INFO ruby 2.0.0 (2013-06-27) [x86_64-darwin12.3.0] == Sinatra/1.4.4 has taken the stage on 4567 for development with backup
In Your Browser Go To: http://localhost:4567 Make sure it works.
3. Edit Your "Gemfile":
In Your "Gemfile": source "https://rubygems.org" gem 'sinatra', '1.1.0'
In Your Terminal Bundle Install: >> bundle install
4. Edit Your "config.ru":
In Your "config.ru" file: require './app' run Sinatra::Application
In Your Terminal Run This To Initiate: >> rackup config.ru [2014-02-24 13:42:37] INFO WEBrick 1.3.1 [2014-02-24 13:42:37] INFO ruby 2.0.0 (2013-06-27) [x86_64-darwin12.3.0] [2014-02-24 13:42:37] INFO WEBrick::HTTPServer#start: pid=6544 port=9292
In Your Browser Go To: http://localhost:9292 Make sure it works.
In Your Terminal:
>> control + c (THIS WILL EXIT)
5. Edit Your "Procfile":
In Your "Procfile" file: web: bundle exec rackup config.ru -p $PORT
In Your Terminal Run This to Initiate >> foreman start 13:44:34 web.1 | started with pid 6550 13:44:36 web.1 | [2014-02-24 13:44:36] INFO WEBrick 1.3.1 13:44:36 web.1 | [2014-02-24 13:44:36] INFO ruby 2.0.0 (2013-06-27) [x86_64-darwin12.3.0] 13:44:36 web.1 | [2014-02-24 13:44:36] INFO WEBrick::HTTPServer#start: pid=6550 port=5000
In Your Browser Go To: http://localhost:5000 Make sure it works.
In Your Terminal:
>> control + c (THIS WILL EXIT)
6. Deploy to Heroku
In Your Root Directory "/
MyCoolSinatraApp
":
>> git init >> heroku --version (I'm assuming you have heroku, but on harm in double checking) >> bundle install (bundle install again, no harm in doing it again) >> git add . >> git commit -m "First commit" (no need to git push if you just want to keep it local) >> heroku apps:create appnamehere (appnamehere will be the name of the url: appnamehere.heroku.com) Creating appnamehere... done, stack is cedar http://appnamehere.herokuapp.com/ | git@heroku.com:appnamehere.git Git remote heroku added >> git remote -v (you should see something like the following) heroku git@heroku.com:appnamehere.git (fetch) heroku git@heroku.com:appnamehere.git (push) >> git push heroku master
7. Weird Things
I usually get the following message right when I initially try to deploy my Sinatra app onto Heroku.
ssh: Could not resolve hostname heroku.com: nodename nor servname provided, or not known
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
However, in my terminal I run the following and the deployment commences with no problem.
>> rackup config.ru
(checking localhost/9292 and then exiting control+c)
>> foreman start
(checking localhost/5000 and then exiting control+c)
>> git push heroku master
(!success!)