Twitter Updates

Saturday, 19 December 2009

Sinatra Second Blog attempt

Example of how to setup a basic Sinatra web application.

This Sinatra app uses erb for the templating and active record for the database ORM. To get the cod eto show up I have had to change "< a" to <-a tags and insert extra space in other tags, unfortunately this means they are not quite cut and paste. To simplify The setup I have created a ./create_db.rb file.

require 'rubygems'
require 'sequel'

# Connect to the database
DB = Sequel.sqlite('./db/blog.db')

unless DB.table_exists? :posts
DB.create_table :posts do
primary_key :id
varchar :title
text :body

# populate the table
DB[:posts].insert(:title => 'Post Man Pat', :body => "has a black and white cat" )
DB[:posts].insert(:title => 'Sandwiches', :body => "Peanut butter and Marmite, Yum")
DB[:posts].insert(:title => 'Leaves', :body => "Grow on trees")

class Posts < Sequel::Model

The Main Application. ./app.rb
require 'rubygems'
require 'sinatra'
require 'active_record'

#Load extra functions
require 'url_logic'

:adapter => 'sqlite3',
:database => './db/blog.db'

class Post < ActiveRecord::Base

$blogpath = "article"

get '/' do
erb :index

#Setup paths for blog
get '/article/?' do
@posts = Post.all()
erb :readallblog

#view a post
get '/article/:id/?' do
@posts = Post.find(params[:id])
erb :showoneblog

#edit a post
get '/article/:id/edit/?' do |id|
@posts = Post.find(params[:id])
erb :editblog

#Update post
put '/article/:id/?' do
@posts = Post.find(params[:id])
@posts.title = params['post']['title']
@posts.body = params['post']['body']
erb :showoneblog

#create a Post
post '/article/?' do
:title =>params['post']['title'] ,
:body => params['post']['body']
@posts = Post.all()
erb :readallblog

#delete an article
delete '/article/?' do
@posts = Post.find(params['post']['id'])
@posts = Post.all()
erb :readblog

readBlog All Posts
<%# $blogpath is global set in the main controller%>
<% for post in @posts %>
< h 1><-a href="/<%=$blogpath%>/<>"><>:<%= post.title %>< / a>

<-a href="/<%=$blogpath%>/<>/edit">edit< / a>
<% end %>

< f orm method="post" action="/<%=$blogpath%>/">
< i nput name="_method" type="hidden" value="post" />
< d iv>
Title< i nput type="text" name="post[title]" value="title"/>

Body< i nput type="text" name="post[body]" value="body"/>

< b utton type="submit">Create
< / d iv>
< / f orm>

Show Single Post

< h 1><>:<%= @posts.title %>< / h 1>
< p ><%= @posts.body%>

<-a href="/<%=$blogpath%>/<>/edit">edit< / a>

Edit Single Post

< h 1><>:<%= @posts.title %>< / h 1>

< f orm method="post" action="/<%=$blogpath%>/<>/">
< i nput name="_method" type="hidden" value="put" />
< d iv>
Title< i nput type="text" name="post[title]" value="<%=@posts.title%>"/>

Body< i nput type="text" name="post[body]" value="<%=@posts.body%>"/>

< b utton type="submit">Update
< / div>
< / f orm>

Hopefully this has covered creating new, editing and retrieving database entries using ActiveRecord. I have probably missed some of the tricks to simplify writing Sinatra apps.

No comments: