-
-
Save mayfer/f72ec07a8d29c6edafae1051baaa0d4c to your computer and use it in GitHub Desktop.
Sinatra tutorials
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' | |
get '/' do | |
title = params[:username] | |
# response.headers["Content-Type"] = "text/plain" | |
response_string = <<-eos | |
<!doctype html> | |
<html> | |
<head> | |
</head> | |
<body> | |
hello #{title} | |
<form method="POST"> | |
<input type="text" name="username" /> | |
</form> | |
</body> | |
</html> | |
eos | |
return response_string | |
end | |
require 'pry' | |
post '/' do | |
"post call, you sent: #{params}" | |
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
require 'sinatra' | |
# set views folder to current directory | |
set :views, Proc.new { File.dirname(__FILE__) } | |
get '/' do | |
title = params[:username] | |
@username = request.cookies["username"] | |
erb :index | |
end | |
get '/:profile/comments' do | |
erb :profile | |
end | |
post '/login' do | |
response.set_cookie("username", params[:username]) | |
redirect "/#{params[:username]}/comments" | |
end | |
get '/logout' do | |
response.set_cookie("username", value: "", expires: Time.now ) | |
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
require 'sinatra' | |
enable :sessions | |
# set views folder to current directory | |
set :views, Proc.new { File.dirname(__FILE__) } | |
get '/' do | |
title = params[:username] | |
@username = session[:username] | |
erb :index | |
end | |
get '/:profile/comments' do | |
erb :profile | |
end | |
post '/login' do | |
# response.set_cookie("username", params[:username]) | |
session[:username] = params[:username] | |
redirect "/" | |
end | |
get '/logout' do | |
session[:username] = nil | |
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
hello <%= @username %> | |
<% if @username %> | |
<br />already logged in lol | |
<a href="/logout">log out</a> | |
<% else %> | |
<form method="POST" action="/login"> | |
<input type="text" name="username" placeholder="username" /> | |
</form> | |
<% 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
<!doctype html> | |
<html> | |
<head> | |
</head> | |
<body> | |
<%= yield %> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment