Skip to content

Instantly share code, notes, and snippets.

@ProfAvery
Created February 9, 2012 07:17
Show Gist options
  • Save ProfAvery/1778068 to your computer and use it in GitHub Desktop.
Save ProfAvery/1778068 to your computer and use it in GitHub Desktop.
Hello World in Sinatra
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>Hi, tell us a little about yourself</h1>
<form method="POST" action="/response">
<p>
Name: <input type="text" name="name" />
</p>
<p>
Profession: <input type="text" name="profession" />
</p>
<input type="submit" />
</form>
</body>
</html>
require 'sinatra'
# Place hello.erb and response.erb in a views/ subdirectory
get '/hello' do
erb :hello
end
post '/response' do
@name = params[:name]
@profession = params[:profession]
erb :response
end
get '/add/*' do
sum = 0
strings = params[:splat].first.split('/')
numbers = strings.map { |s| s.to_i }
numbers.each do |i|
sum += i
end
sum.to_s
end
get '/sum/:a/:b' do
a = params[:a].to_i
b = params[:b].to_i
"#{a} + #{b} = #{a + b}"
end
get '/' do
sum = 2 + 2
"2 + 2 = #{sum}"
end
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>Hello <%= @name %></h1>
Wow, it must be cool to be <%= @profession %>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment