Skip to content

Instantly share code, notes, and snippets.

@sk187
Forked from caseywatts/Gemfile
Last active February 28, 2016 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sk187/18d0516127b769273a3c to your computer and use it in GitHub Desktop.
Save sk187/18d0516127b769273a3c to your computer and use it in GitHub Desktop.
Sinatra on Cloud9

Cloud 9 - Sinatra

Setting Up Cloud9

  • Go to c9.io and log in
  • Create a new workspace
    • Workspace name = hello-world
    • Clone from Git or Mercurial URL = https://gist.github.com/2f5f8bd2fdf583dc757a.git
    • (everything else at defaults)

In Cloud9

Check out the files you have in this folder, see what they have.

In the terminal (at the bottom) type:

  • bundle install
    • this will install the ruby gems you need to have sinatra run
  • ruby hello_world.rb -o $IP -p $PORT
    • this will run the file hello_world.rb, and tell it to make the site available online at a location.
    • Cloud 9 has wired things up so $IP and $PORT contains what it should on their server.
  • In the menu bar, click Preview => Preview Running Application and a browser pane will appear.
  • After each change you make to our files:
    • save the file(s)
    • refresh the browser pane

Templates

  • Make a folder views
  • Add the file views/index.erb with the content
<h1>This is my index.erb file contents oh my.</h1>
  • modify hello_world.rb to use index.erb instead of returning a string.
    • Within the get '/' do: change "Hello World! Welcome to Ruby!" to erb :index.

Pass Params

  • Modify your hello_world.rb get '/' do to include:
  get '/' do 
    num = params['num'].to_i
    @result = num+10
    status 200
    erb :index  
  end
  • Now in your index.erb add this to show the result
  <%= "The result if #{@result}" %>

If we add ?num=10 to the url (mine looks like https://test-sk187.c9users.io/?num=10), the result should 20

  • Now try to modify the code in hello_world.rb's get '/' route to work with the fizzbuzz code we did earlier.
source 'https://rubygems.org'
gem 'sinatra'
gem 'sinatra-contrib'
gem 'json'
require 'sinatra'
require 'sinatra/reloader' if development?
require 'json'
# Commands to run
#
# Things to try
# * have the page display "Howdy!"
# * have the page display some HTML (paste some HTML in the string)
#
# Bonus
# * have the page display the current time (google "ruby current time")
# * have the page display the current time, formatted in a nicer way. (google "ruby strftime" which stands for string-format-time)
get '/' do
"Hello World! Welcome to Ruby!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment