andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I'm trying to follow the tutorial in Jonathan Rockway's talk from OSCON 2007 titled Catalyst in 5 minutes. I'm intrigued by the flexibility Catalysts offers, but apart from the very first splash 'Hello world' page, I'm unable to get his sample up and running. Any help on how to resolve the below issue is appreciated. Apologies for the excessive contents.

# lib/Blog/Controller/Root.pm package Blog::Controller::Root; use strict; use warnings; use base 'Catalyst::Controller::BindLex'; __PACKAGE__->config->{namespace} = ''; sub all_posts : Path : Args(0) { my ($self, $c) = @_; my @posts : Stashed = $c->model('DBIC::Posts')->search( {}, { order_by => 'posted DESC', rows => 5, page => 1, } ); } sub end : ActionClass('RenderView') { }
# lib/Blog/View/TD/Main.pm package Blog::View::TD::Main; use strict; use warnings; use Template::Declare::Tags; sub wrapper(&) { my $content = shift; smart_tag_wrapper { html { head { title { c->stash->{title}; }; }; body { h1 { c->stash->{title} }; $content->(); } } } } template all_posts => sub { c->stash->{title} = 'Blog posts'; wrapper { foreach my $post (@{c->stash->posts||[]}) { div { attr { class => 'post' }; h2 { $post->title }; p { 'Written by ' . $post->author}; }; div { attr { class => 'body'}; outs_raw($post->body); # bad } } } }; 1; __END__
# lib/Blog/Model/DBIC.pm package Blog::Model::DBIC; use strict; use base 'Catalyst::Model::DBIC::Schema'; __PACKAGE__->config( schema_class => 'Blog::Schema', connect_info => [ 'DBI:SQLite:root/catalyst', ], ); 1;
Model and Schema created as follows:
# Create Schema perl script/blog_create.pl model DBIC DBIC::Schema Blog::Schema create +=static DBI:SQLite:root/catalyst # Create View perl script/blog_create.pl view TD Template::Declare
Edit Makefile.PL and make:
# Edit Makefile.PL $ diff Makefile.PL~ Makefile.PL 5a6,7 > requires 'Catalyst::Model::DBIC::Schema'; > requires 'Catalyst::View::Template::Declare'; perl Makefile.PL sudo make installdeps sudo make test
I start the local web-server:
$ perl script/blog_server.pl -r -d [debug] Debug messages enabled [debug] Loaded plugins: .----------------------------------------------------. | Catalyst::Plugin::ConfigLoader 0.18 | | Catalyst::Plugin::Static::Simple 0.20 | '----------------------------------------------------' [debug] Loaded dispatcher "Catalyst::Dispatcher" [debug] Loaded engine "Catalyst::Engine::HTTP::Restarter" [debug] Found home "/home/foo/test/perl/catalyst/blog/Blog" [debug] Loaded Config "/home/foo/test/perl/catalyst/blog/Blog/blog.yml +" [info] Loading subtemplate Blog::View::TD::Main [debug] Loaded components: .-----------------------------------------+----------. | Class | Type | +-----------------------------------------+----------+ | Blog::Controller::Root | instance | | Blog::Model::DBIC | instance | | Blog::View::TD | instance | | Blog::View::TD::Main | class | '-----------------------------------------+----------' [debug] Loaded Private actions: .----------------------+------------------------+--------------. | Private | Class | Method | +----------------------+------------------------+--------------+ | /end | Blog::Controller::Root | end | | /all_posts | Blog::Controller::Root | all_posts | '----------------------+------------------------+--------------' [debug] Loaded Path actions: .-------------------------------------+-------------. | Path | Private | +-------------------------------------+-------------+ | / | /all_posts | '-------------------------------------+-------------' [info] Blog powered by Catalyst 5.7011 You can connect to your server at http://localhost:3000
Opening the url in a web browser yields:
[info] *** Request 1 (0.027/s) [17489] [Mon Oct 22 15:29:38 2007] *** [debug] "GET" request for "/" from "127.0.0.1" [debug] Path is "/" [error] Caught exception in Blog::Controller::Root->all_posts "Can't c +all method "search" on an undefined value at /home/foo/test/perl/cata +lyst/blog/Blog/script/../lib/Blog/Controller/Root.pm line 11." [info] Request took 0.239138s (4.182/s) .----------------------------------------+-----------. | Action | Time | +----------------------------------------+-----------+ | /all_posts | 0.000361s | | /end | 0.002197s | '----------------------------------------+-----------'
Update: I've created the database entries manually as suggested by lorn, but I still get the same error.
$ sqlite3 root/database SQLite version 3.3.6 Enter ".help" for instructions sqlite> .tables posts sqlite> .schema posts CREATE TABLE posts (id integer primary key, title text not null, body +text not null, author text not null, posted date not null); sqlite> select * from posts; 1|test|a new post|Foo|2007-01-01 00:01 2|another test|another post|Bar|2007-01-01 00:02 sqlite>
--
Andreas

Replies are listed 'Best First'.
Re: Basic question on "Catalyst in 5 minutes"
by lorn (Monk) on Oct 22, 2007 at 14:56 UTC

    I think that is something wrong in slides

    perl script/blog_create.pl model DBIC DBIC::Schema Blog::Schema create static DBI:SQLite:root/catalyst

    try to substitute static for dynamic like this:

    perl script/blog_create.pl model DBIC DBIC::Schema Blog::Schema create dynamic DBI:SQLite:root/catalyst UPDATE:

    i try to simulate problem reading the tutorial of the jrockway, and i got this error when i try to create schema:

    lornbook:~/foo/blog/Blog lorn$ perl script/blog_create.pl model DBIC D +BIC::Schema Blog::Schema create=static DBI:SQLite:/tmp/foo exists "/Users/lorn/foo/blog/Blog/script/../lib/Blog/Model" exists "/Users/lorn/foo/blog/Blog/script/../t" DBIx::Class::Schema::Loader::make_schema_at(): No such column create o +n table posts at /usr/local/lib/perl5/site_perl/5.8.8/Catalyst/Helper +/Model/DBIC/Schema.pm line 119

    Did you got this errors? i dont know what is this error, but using 'create dynamic' is not the solution

    UPDATE - 2:

    the problem is a "copy & paste" from the xul slides, i know its weird but, i try to digite CREATE TABLES posts... manually, and everything works now, when i start the App, they show me the tables posts 'Blog::Model::DBIC::Posts' :) ( in your posts they show only 'Blog::Model::DBIC')

      Hi!

      I think I have found a bug in DBIx::Class::Schema::Loader, which relates to this copy&paste problem here.

      It seems that DBIx::Class::Schema::Loader does not like SQL constructs like this:

      CREATE TABLE foo
      (id INTEGER);
      
      but is happy with constructs like this:
      CREATE TABLE foo(
      id INTEGER);
      
      The problem seems to be where the opening parentheses lies.

      N.B. that SQLite is happy with either one.

      For what I can see from the Catalyst in 5 minutes presentation, the opening parentheses there is in a new line, and hence running

      $ script/blog_create model Blog DBIC::Schema Blog create=static dbi:SQLite:database.db
      
      is doomed to fail.

      Please, confirm that you can reproduce the situation. I intend to fill a bug report against DBIx::Class::Schema::Loader.

      Hope that it helps, fellow mongers.

      Update: The bug belonged in DBD-SQLite: bug #34408, and there is already a patch which solves the issue. Thanks, ilmari!