Tuesday Apr 28, 2009

TOTD #80: Sinatra CRUD application using Haml templates with JRuby and GlassFish Gem


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:

/tools/jruby-1.2.0 >./bin/jruby -S gem install haml --no-ri --no-rdoc
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Successfully installed haml-2.0.9
1 gem installed

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!
  1. 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)

  2. 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!"
    end

    get '/' do
      @runner = Runner.find(:all)
      haml :index
    end

    get '/new' do
      haml :new
    end

    get '/:id' do
      @runner = Runner.find(params[:id])
      if (@runner)
        haml :show
      else
        redirect '/'
      end
    end

    post '/' do
      @runner = Runner.new(:distance => params[:distance], :minutes => params[:minutes])
      if @runner.save
        redirect "/#{@runner.id}"
      else
        redirect '/'
      end
    end

    Firstly, 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:
    1. 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.
    2. One POST method to save the newly created log entry in the database.
  3. 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".
    1. "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

    2. "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

    3. "show.haml": Show a particular log entry

      %h1 Showing a runner log ...
      Distance:
      = @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
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

Monday Apr 21, 2008

TOTD #30: CRUD Application using Grails - Hosted on Jetty and HSQLDB


After a simple Grails application, let's create a CRUD application. Such an application allows to perform basic database operations to read table rows from the database, create new rows, and edit and delete existing rows. This blog shows how such an application can be created using Grails, hosted on built-in Jetty servlet engine and use in-memory HSQLDB database for persistence.

A follow-up entry will show how this application can be deployed in production mode on GlassFish and using MySQL database.
  1. Create a Grails application
    1. After Grails download and configuration, create a new application "crud" as:

      ~/testbed/grails-1.0.2/samples >grails create-app crud

      Welcome to Grails 1.0.2 - http://grails.org/
      Licensed under Apache Standard License 2.0
      Grails home is set to: /Users/arungupta/testbed/grails-1.0.2

      Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples
      Note: No plugin scripts found
      Running script /Users/arungupta/testbed/grails-1.0.2/scripts/CreateApp.groovy
      Environment set to development
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/src
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/src/java
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/src/groovy
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/controllers
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/services
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/domain
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/taglib
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/utils
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/views
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/views/layouts
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/i18n
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/conf
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/test
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/test/unit
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/test/integration
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/scripts
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/js
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/css
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/images
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/META-INF
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/lib
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/conf/spring
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/conf/hibernate
      [propertyfile] Creating new property file: /Users/arungupta/testbed/grails-1.0.2/samples/crud/application.properties
           [copy] Copying 2 files to /Users/arungupta/testbed/grails-1.0.2/samples/crud
           [copy] Copying 2 files to /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/WEB-INF
           [copy] Copying 5 files to /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/WEB-INF/tld
           [copy] Copying 87 files to /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app
           [copy] Copying 17 files to /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app
           [copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud
           [copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud
           [copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud
      [propertyfile] Updating property file: /Users/arungupta/testbed/grails-1.0.2/samples/crud/application.properties
      Created Grails Application at /Users/arungupta/testbed/grails-1.0.2/samples/crud
    2. In your project directory, create a domain class as:

      ~/testbed/grails-1.0.2/samples/crud >grails create-domain-class state

      Welcome to Grails 1.0.2 - http://grails.org/
      Licensed under Apache Standard License 2.0
      Grails home is set to: /Users/arungupta/testbed/grails-1.0.2

      Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples/crud
      Note: No plugin scripts found
      Running script /Users/arungupta/testbed/grails-1.0.2/scripts/CreateDomainClass.groovy
      Environment set to development
           [copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/domain
      Created  for State
           [copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud/test/integration
      Created Tests for State

      This creates "State.groovy" class in "grails-app/domain" directory and looks like:

      class State {

      }

      Add two fields to store state name and the corresponding abbreviation as shown below:

      class State {
              String name
              String abbrev
      }
    3. Change "grails-app/conf/BootStrap.groovy" class to initialize the domain with sample data as shown below:

      class BootStrap {

           def init = { servletContext ->
                new State(name:"California", abbrev:"CA").save()
                new State(name:"New York", abbrev:"NY").save()
                new State(name:"Texas", abbrev:"TX").save()
                new State(name:"Wisconsin", abbrev:"WI").save()
           }
           def destroy = {
           }
    4. Create a new controller as:

      ~/testbed/grails-1.0.2/samples/crud >grails create-controller state

      Welcome to Grails 1.0.2 - http://grails.org/
      Licensed under Apache Standard License 2.0
      Grails home is set to: /Users/arungupta/testbed/grails-1.0.2

      Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples/crud
      Note: No plugin scripts found
      Running script /Users/arungupta/testbed/grails-1.0.2/scripts/CreateController.groovy
      Environment set to development
           [copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/controllers
      Created Controller for State
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/views/state
           [copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud/test/integration
      Created ControllerTests for State

      This generates "grails-app/controllers/StateController.groovy" with the following content:

      class StateController {

          def index = { }
      }

      Replace it with to define a scaffold:

      class StateController {

          def scaffold = State
      }
  2. Start the application using built-in Jetty Servlet engine and in-memory HSQLDB database as shown below:

    ~/testbed/grails-1.0.2/samples/crud >grails run-app

    Welcome to Grails 1.0.2 - http://grails.org/
    Licensed under Apache Standard License 2.0
    Grails home is set to: /Users/arungupta/testbed/grails-1.0.2

    Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples/crud
    Note: No plugin scripts found
    Running script /Users/arungupta/testbed/grails-1.0.2/scripts/RunApp.groovy
    Environment set to development
    Running Grails application..
    2008-04-18 17:26:29.962::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
    2008-04-18 17:26:29.075::INFO:  jetty-6.1.4
    2008-04-18 17:26:29.155::INFO:  No Transaction manager found - if your webapp requires one, please configure one.
    2008-04-18 17:26:30.886:/crud:INFO:  Set web app root system property: 'crud' = [/Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/]
    2008-04-18 17:26:30.886:/crud:INFO:  Initializing Log4J from [file:/Users/arungupta/.grails/1.0.2/projects/crud/resources/log4j.properties]
    2008-04-18 17:26:30.945:/crud:INFO:  Initializing Spring root WebApplicationContext
    [0] spring.GrailsWebApplicationContext Refreshing org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@848dfb: display name [org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@848dfb]; startup date [Fri Apr 18 17:26:32 PDT 2008]; parent: org.springframework.web.context.support.XmlWebApplicationContext@cddcc3
    [1] spring.GrailsWebApplicationContext Bean factory for application context [org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@848dfb]: org.springframework.beans.factory.support.DefaultListableBeanFactory@11f136
    2008-04-18 17:26:34.655:/crud:INFO:  Initializing Spring FrameworkServlet 'grails'
    2008-04-18 17:26:35.716::INFO:  Started SelectChannelConnector@0.0.0.0:8080
    Server running. Browse to http://localhost:8080/crud

    The application is now accessible at "http://localhost:8080/crud" and looks like:



  3. READ - Click on "StateController" and the following page is shown:



  4. CREATE - Click on "New State" and enter the values as shown below:



    and click on "Create" to see the following page:



  5. UPDATE & DELETE - You can click on "Edit" or "Delete" to perform U or D of CRUD or click on "State List" to view the updated list as shown below:

Please leave suggestions on other TOTD that you'd like to see. A complete archive is available here.

Technorati: groovy grails glassfish jetty hsqldb scripting crud

Tuesday Feb 19, 2008

TOTD #28: Getting Started with Rails 2.0 Scaffold

Rails 2.0 changes the way Scaffold works. This blog walks you through the steps to create a simple CRUD application using Scaffold in Rails 2.0.
  1. Download & Install JRuby 1.1 RC2.
  2. Install Rails using the following command:

    jruby -S gem install rails
  3. Create a new Rails app using the following command:

    cd samples; mkdir rails; cd rails
    jruby -S rails books -d mysql
  4. Start MySQL server in a different shell using the following command:

    sudo /usr/local/mysql/bin/mysqld_safe --console
  5. Creat the database using the following command:

    cd books
    jruby -S rake db:create

    This creates the database defined by RAILS_ENV (Development is default). Here are some other new database-related commands:

    db:create:all Create all the databases (_Development, _Test, _Production)
    db:drop Drops your database
    db:reset Drop and Re-create your database, including migrations
  6. Generate a scaffold using the following command:

    jruby script/generate scaffold book title:string author:string isbn:string description:text


    The output of the command looks like:

          exists  app/models/
          exists  app/controllers/
          exists  app/helpers/
          create  app/views/books
          exists  app/views/layouts/
          exists  test/functional/
          exists  test/unit/
          create  app/views/books/index.html.erb
          create  app/views/books/show.html.erb
          create  app/views/books/new.html.erb
          create  app/views/books/edit.html.erb
          create  app/views/layouts/books.html.erb
          create  public/stylesheets/scaffold.css
      dependency  model
          exists    app/models/
          exists    test/unit/
          exists    test/fixtures/
          create    app/models/book.rb
          create    test/unit/book_test.rb
          create    test/fixtures/books.yml
          create    db/migrate
          create    db/migrate/001_create_books.rb
          create  app/controllers/books_controller.rb
          create  test/functional/books_controller_test.rb
          create  app/helpers/books_helper.rb
          route  map.resources :books


    There is no need to create the model explicitly as was the case in previous version of Rails. This creates the "db/migrate/001_create_books.rb" migration which looks like:

    class CreateBooks < ActiveRecord::Migration
      def self.up
        create_table :books do |t|
          t.string :title
          t.string :author
          t.string :isbn
          t.text :description

          t.timestamps
        end
      end

      def self.down
        drop_table :books
      end
    end
  7. Create the database tables using the following command:

    jruby -S rake db:migrate
  8. Deploy the application on WEBrick using the following command:

    jruby script/server


    The application is now available at "http://localhost:3000/books" and looks like:

    Rails2 CRUD Blank Page
  9. Click on "New book" to see a page as shown below (with values entered):

    Rails2 CRUD New Entry
    Click on Create button. After 2 entries have been entered, it looks like as shown below:

    Rails 2 CRUD Multiple Entries
That's it, you've created  a simple Rails 2.0 CRUD application.

You can also deploy this application easily on GlassFish v3 gem. Just follow the instructions here and enjoy!

I'll post a follow up blog where this is much more simplifed using NetBeans 6.1 builds where JRuby 1.1 and Rails 2.0.2 are already integrated.

Technorati: totd ruby jruby rubyonrails rails2 scaffold crud netbeans glassfish v3 gem

Wednesday Nov 07, 2007

Screencast #Web10: CRUD using jMaki and JPA

This screencast shows how to create a simple jMaki application, using NetBeans IDE, that performs some of the CRUD operations on a Data Table widget. It uses Java Persistence API (JPA) to connect to the database and the application is deployed on GlassFish. The rest of the CRUD operations can be easily built using the same methodology.

The steps followed in this screencast are also described in detail.

Enjoy it here!

Technorati: screencast jmaki netbeans glassfish jpa database crud

About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today