Created
February 9, 2012 07:17
-
-
Save ProfAvery/1778068 to your computer and use it in GitHub Desktop.
Hello World in Sinatra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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