Skip to content
dannyglue edited this page Dec 20, 2010 · 7 revisions

Form

Assuming that we have a web server, e.g. Apache running locally, and we have the Postgres database and tables created according to Generate Database Tables with Rose::DBx::Object::Builder, and we want to generate a form for adding positions, we can write the following CGI script

#!/usr/bin/perl
use strict;
use warnings;

use Rose::DBx::Object::Renderer;

use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $query = new CGI;
print $query->header();

my $renderer = Rose::DBx::Object::Renderer->new(
  config => {
    db => {type => 'Pg', name => 'demo', username => 'postgres', password => '', tables_are_singular => 1},
    template => {path => '../templates'},
    upload => {path => '../htdocs/uploads', url => '/uploads'},
  },
  load => 1,
);

Demo::Position->render_as_form();

That is everything we need to generate a web form that save positions into the underlying database.

Example of the generated form: http://db.tt/kxcW1eN

Table

We can of course use a table for CRUD. Simply replace the render_as_form() line with:

Demo::Position::Manager->render_as_table(
  create => 1,
  edit => 1,
  copy => 1,
  delete => 1,
  searchable => 1,
);

Example of the generated table: http://db.tt/cl5183g

Menu

If we want to generate a tab menu to include all the tables for CRUD, we can use the render_as_menu() instead

Demo::Employee::Manager->render_as_menu(
  order => ['Demo::Employee', 'Demo::Position', 'Demo::Project'],
  create => 1,
  edit => 1,
  copy => 1,
  delete => 1,
  searchable => 1,
);

Examples of the generated menu UI:

Complete Source
#!/usr/bin/perl
use strict;
use warnings;

use Rose::DBx::Object::Renderer;

use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $query = new CGI;
print $query->header();

my $renderer = Rose::DBx::Object::Renderer->new(
  config => {
    db => {type => 'Pg', name => 'demo', username => 'postgres', password => '', tables_are_singular => 1},
    template => {path => '../templates'},
    upload => {path => '../htdocs/uploads', url => '/uploads'},
  },
  load => 1,
);

Demo::Employee::Manager->render_as_menu(
  order => ['Demo::Employee', 'Demo::Position', 'Demo::Project'],
  create => 1,
  edit => 1,
  copy => 1,
  delete => 1,
  searchable => 1,
);