Created
February 23, 2015 18:56
-
-
Save mayfer/a006357d67a9609233b8 to your computer and use it in GitHub Desktop.
Intro to Sinatra
This file contains hidden or 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> | |
<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> |
This file contains hidden or 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' | |
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 | |
This file contains hidden or 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
* 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