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.

require 'rubygems'
require 'sinatra'
get '/hi' do
"Hello World!"

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

Then checkout

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');

require 'rubygems'
require 'sinatra'
require 'active_record'

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

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

Now create ./views/index.erb

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

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

No comments: