TOTD #79 showed how to run a trivial Sinatra application using GlassFish Gem. Sinatra provides support for Haml, Erb, Builder, Sass, and Inline templates as described here. This TOTD will show how to get started with creating a Sinatra CRUD application using Haml templates.
Haml is based on one primary principle – Markup should be beautiful because beauty makes you faster.
Get started by installing the Haml gem as:
And follow the tutorial, documentation, and reference page for more details. |
Sinatra is ORM-agnostic and so any Ruby ORM framework such as ActiveRecord, DataMapper, Sequel, and others. DataMapper with JRuby requires work so this TOTD will show how to use ActiveRecord instead. There is sinatras-hat which allows to create RESTy CRUD apps with Sinatra. There probably are mutiple other ways to create this application but I prefer to understanding the wiring so this blog will use a bare minimal structure.
Anyway, lets get started!
- Create the database as:
~/tools/jruby/samples/sinatra-sample >mysql –user root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 664
Server version: 5.1.30 MySQL Community Server (GPL)Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
mysql> create database hello_development;
Query OK, 1 row affected (0.00 sec)mysql> use hello_development;
Database changed
mysql> CREATE TABLE `runners` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `distance` float, `minutes` int(11), `created_at` datetime, `updated_at` datetime);
Query OK, 0 rows affected (0.06 sec) - Update “hello.rb” from TOTD #79 such that it looks like:
require ‘rubygems’
require ‘sinatra’
require ‘activerecord’## Setup
ActiveRecord::Base.establish_connection(
:adapter => “jdbcmysql”,
:host => “localhost”,
:username => “root”,
:password => “”,
:database => “hello_development”
)## Models
class Runner < ActiveRecord::Base
end## Controller Actions
get ‘/hi’ do
“Hello World!”
endget ‘/’ do
@runner = Runner.find(:all)
haml :index
endget ‘/new’ do
haml :new
endget ‘/:id’ do
@runner = Runner.find(params[:id])
if (@runner)
haml :show
else
redirect ‘/’
end
endpost ‘/’ do
@runner = Runner.new(:distance => params[:distance], :minutes => params[:minutes])
if @runner.save
redirect “/#{@runner.id}”
else
redirect ‘/’
end
endFirstly, it pulls in the ActiveRecord dependency. Then “ActiveRecord::Base.establish_connection” is used to establish a connection with the previously created database. “Runner” is tagged as a model class by inheriting from “ActiveRecord::Base” and uses the default table name (“runners” in this case). Add four new methods:
- Three GET methods to show all the runners, a form to enter new data, and show a particular log entry. Each method requires a HAML template (will be created in next step) to render the information.
- One POST method to save the newly created log entry in the database.
- Create a new directory “views” and create the following files in that directory. Each file serves as a view and rendered from an action in “hello.rb”.
- “index.haml”: Show all the runners
%h1 Listing all runners …
%table
%tr
%th Distance
%th Minutes
– @runner.each do |r|
%tr
%td= r.distance
%td= r.minutes
%br
%a{:href=>”/new”}
New Runner - “new.haml”: Enter a new entry
%h1 Adding a new runner log …
%form{:method=>”post”, :action=>”/”}
Distance:
%input{:type=>”text”, :name=>”distance”}
%br
Minutes:
%input{:type=>”text”, :name=>”minutes”}
%br
%input{:type=>”submit”, :value=>”Submit”}
%br - “show.haml”: Show a particular log entry
%h1 Showing a runner log …
Dis
tance:
= @runner.distance
%br
Minutes:
= @runner.minutes
%br
%br
%a{:href=>”/”}= “Show All!”The complete directory structure looks like:
.
./hello.rb
./views
./views/index.haml
./views/new.haml
./views/show.haml
- “index.haml”: Show all the runners
That’s it, now run the application as:
~/tools/jruby/samples/sinatra-sample >../../bin/jruby -S glassfish Starting GlassFish server at: 192.168.1.145:3000 in development environment… Writing log messages to: /Users/arungupta/tools/jruby-1.2.0/samples/sinatra-sample/log/development.log. Press Ctrl+C to stop. |
The main page is available at “http://localhost:3000/” and looks like:
Clicking on “New Runner” gives …
Enter the data, and click on “Submit” to show …
Click on “Show All!” to see all the entries added so far …
And after adding few entries the main page looks like …
This application shows Create and Read from the CRUD, it’s fairly easy to add Update and Delete functionality as well but that’s an excercise left for the readers
You’ll hear all about it at Develop with Pleasure, Deploy with Fun: GlassFish and NetBeans for a Better Rails Experience at Rails Conf next week.
Technorati: totd glassfish jruby sinatra crud
Related posts:- TOTD #79: Getting Started with Sinatra applications on JRuby and GlassFish Gem
- TOTD #53: Scaffold in Merb using JRuby/GlassFish
- TOTD # 70: JRuby and GlassFish Integration Test# 1: JRuby 1.2.0 RC1 + Rails 2.2.x + GlassFish Gem
- TOTD #110: JRuby on Rails application using Oracle on GlassFish
- Getting Started with Edge Rails (2.2) using JRuby and GlassFish
Hi,
thanks for blogging about Haml support in Netbeans. Still, may be you can report to Sun (Oracle! ouch) guys that on Netbeans daily builds (faster Ruby edition) the HAML plugin is a bit trikky to install. The Netbeans plugin won’t install I believe, at least for me I had to copy it by hand in the Ntebeans local module repo + hack some XML files I think.
Aside from that I just switch to HAML and I totally recommend it. Views don’t suck any more with HAML. So Netbeans support to HAML is very important.
Comment by Raphaël Valyi — April 29, 2009 @ 3:45 am
Thanks Raphael!
Your comment made me aware of HAML support in NetBeans. Anyway I’ve sent the feedback to the concerned folks. Have you tried filing a bug at:
http://www.netbeans.org/issues/enter_bug.cgi?component=ruby
Comment by Arun Gupta — April 29, 2009 @ 4:08 pm
[Trackback] This is a follow up post from David’s keynote. Attended Women in Rails panel discussion. The panel, Sarah Mei, Lori Olson, and Desi McAdam (from L to R), had a very interesting discussion around the genuine problems and possible solutions…
Comment by Arun Gupta's Blog — May 6, 2009 @ 5:40 am
Ruby and Running, a good combination!
Definately going to try this one out
Comment by waveninja — May 18, 2009 @ 7:26 am
[Trackback] If you are using Warbler to create a WAR file of your application and deploying on GlassFish or any other Servlet container, then you are likely seeing the following error during deployment: [#|2009-07-30T15:29:50.788-0700|SEVERE|sun-appserver2.1|java…
Comment by Arun Gupta's Blog — July 31, 2009 @ 5:54 am