Twitter Updates

Tuesday 15 December 2009

Sinatra First Blog attempt

After getting Xcode for Snow leopard correctly installed and restarting. The basic sinatra works correctly.

hi.rb
----
require 'rubygems'
require 'sinatra'
get '/hi' do
"Hello World!"
end

Command line:
$ gem install sinatra
$ ruby hi.rb

Then checkout 127.0.0.1:4567.

Turning it an almost blog, with some erb and an sqlite 3 database.
Assuming you have macports installed you can do (or look up installing sqlite3):
$ sudo port install sqlite3
cd Sinatra_dir
sqlite3 blog
#you are now in the sqlite command prompt
sqlite> create table posts (title text, body text );
sqlite> insert into posts values('title1','Very long article');
sqlite> insert into posts values('Sinatra Blog Post','text text text text text');

hi.rb
----
require 'rubygems'
require 'sinatra'
require 'active_record'

ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:dbfile => 'blog.db'
)
class Post < ActiveRecord::Base
end

get '/news/?' do
@posts = Post.all()
erb :index
end


Now create ./views/index.erb
----
HelloWorld!

<% for post in @posts %>
< h 1><%= post.title %>< / h 1>
< p ><%= post.body%>< / p>
<% end %>


$ ruby hi.rb and goto 127.0.0.1:4567.
If all goes well you should see some thing like this:

No comments: