All Articles

Ruby Todolist App

Hello guys, its been a while since i last blogged about coding related stuff.
Now, I'd like to show you How simple and fun The Ruby Programming Language is.

I wrote Todolist App with Sinatra (web framework) + DataMapper (db persistent) + Haml (view template) The total number of lines is just only 41, in a single file including the view template :)

require 'sinatra'
require 'dm-core'
require 'dm-migrations'
require 'haml'

class Todo
  include DataMapper::Resource
  property :id, Serial
  property :text, String
end

configure do
  DataMapper.setup(:default, "sqlite:///development.sqlite3")
  DataMapper.auto_migrate!
end

get '/' do
  @todos = Todo.all
  haml :index
end

post '/' do
  Todo.create(:text => params['todo'])
  redirect '/'
end

__END__

@@ index
!!!
%html
	%head
		%title Todo
	%body
		%h1 Todo
		%ul
		-	@todos.each do |todo|
			%li= todo.text
		%form{:action => '/', :method => 'POST'}
			%input{:type => 'text', :name => 'todo'}
			%input{:type => 'submit', :value=> 'Todo!'}