Skip to content

Instantly share code, notes, and snippets.

@nizaroni
Created April 7, 2015 17:03
Show Gist options
  • Save nizaroni/c91268062bd479607ccd to your computer and use it in GitHub Desktop.
Save nizaroni/c91268062bd479607ccd to your computer and use it in GitHub Desktop.
Sinatra Twitter example.
source 'https://rubygems.org'
gem 'sinatra'
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..."
<!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>
<!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