[Catalyst] FOREACH problem

Dmitri Pissarenko dmitri.pissarenko at gmail.com
Sun May 6 15:39:23 GMT 2007


Hello!

I have an action, which should do following:

It reads from the GET parameter a string with record IDs, i. e. "1,2,3,".

Then, this action should fetch from the database the records with these IDs.

Finally, it should pass the resulting array to another page, in which
these records are displayed using

[% FOREACH cat IN categoriesToDelete -%]
  <li>[% cat.name %]</li>
[% END -%]

Now I have the problem that in the page with FOREACH nothing is
displayed, even though categoriesToDelete contains some data.

The code is this:

sub form_deleteSeveralCategories : Local {
    my ($self, $c) = @_;

    # fetch IDs as string
    my $categoryIdsAsSingleString = $c->request->params->{categoryIds};

    # convert string to array of individual IDs
    my @categoryIds = split (/,/,  $categoryIdsAsSingleString);

    # read records from the database and store them in @categoriesToDelete
    my @categoriesToDelete = map
    {
    	# $_ is the ID of the record
	    $c->model('TimeTrackingAppDB::Category')->find($_);
    } @categoryIds;

    foreach (@categoriesToDelete)
    {
	    $c->log->debug('cat: ' . $_);
    };

    # pass the parameters to the page with FOREACH
    $c->stash->{categoryIds} = $categoryIdsAsSingleString;
    $c->stash->{categoriesToDelete} = @categoriesToDelete;

    # Set the TT template to use
    $c->stash->{template} = 'categories/form_deleteSeveralCategories.tt2';
}

form_deleteSeveralCategories.tt2 has following content:

[% META title = 'Manual Form Category Delete' -%]

<form name="deleteCategoriesForm" method="post" action="[%
Catalyst.uri_for('form_deleteSeveralCategories_do') %]">
<input type="hidden" name="categoryIds" value="[% categoryIds %]">
Do you really want to delete categories listed below?

<ul>
[% FOREACH cat IN categoriesToDelete -%]
  <li>[% cat.name %]</li>
[% END -%]
</ul>
[% categoriesToDelete %]
<a href="javascript:document.deleteCategoryForm.submit();">Yes</a> <a
href="javascript:self.close();">No</a>
</form>

categoriesToDelete is passed to form_deleteSeveralCategories.tt2 very
well, since "[% categoriesToDelete %]" is displayed as "2" (number of
elements in the array).

But nothing in displayed in the FOREACH loop.

What am I doing wrong?

TIA

Dmitri Pissarenko



More information about the Catalyst mailing list