1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
sinatra/examples/todo/todo.rb
2007-10-04 15:33:55 -07:00

38 lines
1,007 B
Ruby

$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib'
require 'sinatra'
get '/' do
@items = session[:items] || []
haml <<-haml
%script window.document.getElementById('new_item').focus();
%h1 Sinatra's todo list
%ul
- @items.each_with_index do |item, index|
%li.item
%div
= item
%form{:action => "/" + index.to_s, :method => 'POST'}
%input{:type => 'hidden', :name => '_method', :value => 'DELETE'}
%input{:type => 'submit', :value => 'delete'}
%form{:action => '/clear', :method => 'POST'}
%input{:value => 'clear', :type => :submit}
%form{:action => '/', :method => 'POST'}
%input{:type => 'textbox', :name => :new_item, :id => 'new_item'}
%input{:type => 'submit'}
haml
end
post '/' do
(session[:items] ||= []) << params[:new_item] unless params[:new_item].to_s.strip.empty?
redirect '/'
end
post '/clear' do
session[:items].clear
redirect '/'
end
delete '/:id' do
session[:items].delete_at(params[:id].to_i)
redirect '/'
end