Skip to content

Instantly share code, notes, and snippets.

@mayfer
Created February 23, 2015 18:56
Show Gist options
  • Save mayfer/a006357d67a9609233b8 to your computer and use it in GitHub Desktop.
Save mayfer/a006357d67a9609233b8 to your computer and use it in GitHub Desktop.
Intro to Sinatra
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="/style.css" />
</head>
<body>
<% if @loggedin == true %>
welcome, <%=@username %>
<a href='/logout'>Log out</a>
<% else %>
<form action="/login" method="post">
<input type="text" name="username" /><br />
<input type="password" name="password" /><br />
<input type="submit" value="Submit" />
</form>
<% end %>
</body>
</html>
require 'sinatra'
set :views, File.dirname(__FILE__)
get '/' do
@username = request.cookies["username"]
if @username
@loggedin = true
else
@loggedin = false
end
erb :'new.html'
end
post '/login' do
response.set_cookie("username", :value => params[:username], :path => "/", :expires => Time.now + 60*60*24*365*3)
redirect '/'
end
get '/logout' do
response.set_cookie("username", :value => "", :path => "/", :expires => Time.now - 86400000)
redirect '/'
end
* You can run the sinatra app with "ruby new.rb" or "shotgun new.rb" to auto-reload the server with code changes.
* Put your style.css and other static files in a folder called "public" in the same directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment