NB. Actually, this code works fine, I just has a bug in my routes. I'll leave the question here in case it's of use to someone...
I'm not sure how to access the results of a search within Mojolicious templates
For example, I've tried this, which doesn't work:
sub list {
my $self= shift;
#Return a list of questions
my @list = $self->db->resultset('Question')->search({}, {order_by => { -desc => 'q_order' }});
$self->stash(list => \@list);
}
Then in my template is
% for my $item (@$list) {
<%= $item->question %>
<%= $item->explanation %> <br />
% }
However this gives the error
Not an ARRAY reference at template line x (where line x is the line containing @$list)
i've tried a variety of other things.
If I call search in list context and dump the results, I can see I get a list of 'Schema::Result::Question' objects - that's right.
I'm just not sure how to loop through and access the data in the template?
returns a resultset.
You can call ->all on it like so ( you missed the
->all
)OR, you can iterate through the resultset like so :
This does not immediately fetch all those questions, but, as it is a resultset, it only executes when called upon ( i.e. you start the iteration ).
This may be an advantage to the second method.