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

I trying to get into development with Catalyst and don't know if this is the best place for questions on Catalyst or not but if someone here is able to help me out I would be greatfull.

I am trying to pull data out of my database and display it in my View (Template Toolkit), farily common right? My model is configured to work with my PostgreSQL server and I have my controller set as far as I can tell but the view doesn't display anything.

Here is my sub I am working with from my controller:

sub list :Local { my ($self, $c) = @_; $c->stash(users => [$c->model('DB::tech')->all]); $c->stash(template => 'admin/list.tt2'); }

See below for updated code

This is my code for my view

[% META title = 'System Users List' -%] <table> <tr><th>ID</th><th>First Name</th><th>Last Name</th><th>Email</th><th> +Phone</th><th>Management Rating</th><th>Comments</th><th>Date</th><th +>Is Admin</th></tr> [% FOREACH tech in tech -%] <tr> <td>[% users.id %]</td> <td>[% users.firstname %]</td> <td>[% users.lastname %]</td> <td>...</td> ....snip... </tr> [% END -%] </table>

In the debug output from DBIC_TRACE I can see the query

SELECT me.uniqid, me.id, me.firstname, me.lastname, me.email, me.phone, me.managementrating, me.managementcomments, me.date, me.ismanagement FROM tech me;

That looks correct to me, I'm sure this issue is being casused by something small.

Replies are listed 'Best First'.
Re: Need help working with a view in Catalyst
by chromatic (Archbishop) on Mar 12, 2011 at 22:08 UTC

    Perhaps you mean:

    [% FOREACH user in users -%] <tr> <td>[% user.id %]</td> <td>[% user.firstname %]</td> <td>[% user.lastname %]</td> <td>...</td> ....snip... </tr> [% END -%]
Re: Need help working with a view in Catalyst
by Your Mother (Archbishop) on Mar 12, 2011 at 22:13 UTC

    If this is actually what you have–

    [% FOREACH tech in tech -%]

    –it should be (updated from what chromatic noticed)–

    [% FOREACH tech IN users -%]

    And FOR is more “perly.” I would probably also pass $c->model('DB::tech')->search_rs instead and then it becomes–

    [% FOR tech IN users.all -%]

    You have access to things like the pager and such passing the resultset around instead of a list of objects + deferred queries can sometimes be a win.

      Following your suggestion of going with the for the data from my controller still needs to be places in the stash correct? The way I am currently doing it in my Controller is:

      $c->stash(users => [$c->model('DB::tech')->search_rs]);

      and in my template I am calling it via

      [% FOR tech IN users.all -%]

      but yet it still is not displaying any data from my database. There is one row in the table that I am trying to work with currently

      repairs=> select * from tech; + uniqid | id | firstname | lastname | email | phone | managementrating + | managementcomments | dat e | ismanagement + --------+----+-----------+----------+-------+-------+----------------- +-+--------------------+------- -----+-------------- + 0 | 99 | Admin | | | | 10 + | | 1999-0 1-01 | t + (1 row)

        Close. You don't want to take an array reference. The result set is already a reference to an object. Just change that–

        $c->stash( users => $c->model('DB::tech')->search_rs );

        –and you should be good. You might want to look around for advice and techniques with chained controller methods too. They go very well with chained result sets and can make code terse while still being easy to read and follow.