Created
April 7, 2015 17:03
-
-
Save nizaroni/c91268062bd479607ccd to your computer and use it in GitHub Desktop.
Sinatra Twitter example.
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
source 'https://rubygems.org' | |
gem '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
require 'sinatra' | |
tweets = [] | |
get '/' do | |
@tweets = tweets | |
erb(:index) | |
end | |
post '/save-tweet' do | |
# Add new tweet to the array | |
tweets.push(params[:tweet]) | |
# redirect back to index | |
redirect to('/') | |
end | |
get '/success' do | |
erb(:success) | |
end | |
# "I like pie", | |
# "I'm learning ERBs", | |
# "Yesterday's lunch was great.", | |
# "WEEKEND IS HERE.", | |
# "And so on..." |
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> | |
<title>Routing Example</title> | |
</head> | |
<body> | |
<form method="POST" action="/save-tweet"> | |
<textarea name="tweet" placeholder="What's on your mind?"></textarea> | |
<button type="submit">Send</button> | |
</form> | |
<ol> | |
<% @tweets.each do |twt| %> | |
<li class="tweet"> | |
<img src="/profile.png"> | |
<p><%= twt %></p> | |
</li> | |
<% end %> | |
</ol> | |
</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
<!doctype html> | |
<html> | |
<head> | |
<title>Routing Example</title> | |
</head> | |
<body> | |
<h1>Your tweet was posted successfully!</h1> | |
<a href="/">Back to home</a> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment