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.

./create_db.rb
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
end

# 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")
end

class Posts < Sequel::Model
end


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

#Load extra functions
require 'url_logic'

ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => './db/blog.db'
)

class Post < ActiveRecord::Base
end

$blogpath = "article"

get '/' do
erb :index
end

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

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

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

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


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

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


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

<-a href="/<%=$blogpath%>/<%=post.id%>/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>

./views/showoneblog.erb
Show Single Post

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

<-a href="/<%=$blogpath%>/<%=@posts.id%>/edit">edit< / a>



./views/blog
Edit Single Post

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

< f orm method="post" action="/<%=$blogpath%>/<%=@posts.id%>/">
< 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: