- Go to
c9.ioand log in - Create a new workspace
Workspace name=hello-worldClone from Git or Mercurial URL=https://gist.github.com/2f5f8bd2fdf583dc757a.git- (everything else at defaults)
Check out the files you have in this folder, see what they have.
In the terminal (at the bottom where it says bash) type:
bundle install- this will install the ruby
gems listed in theGemfilethat you need to have sinatra run
- this will install the ruby
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
$IPand$PORTcontains what it should on their server.
- In the menu bar, click
Preview=>Preview Running Applicationand a browser pane will appear. - After each change you make to our files:
- save the file(s)
- refresh the browser pane
- Make a folder
views - Add the file
views/index.erb(a file namedindex.erbin the folderviews/) with the content
<h1>This is my index.erb file contents oh my.</h1>
- modify
hello_world.rbto useindex.erbinstead of returning a string.- Within the
get '/' do: change"Hello World! Welcome to Ruby!"toerb :index.
- Within the
- Modify your
hello_world.rbget '/' doto 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.
Get your FizzBuzz code from earlier to work on Sinatra.
- Goal: You go to
mysite.com/?num=10and it displays on the page whether it isfizz,buzz, etc. For example10: Fizz. - You'll want to pass the result
fizzorbuzzto the template by assigning it to the variable@result. - You'll want to use a function
def fizzbuzz... that returns a value