Facebook
From Gruff Parakeet, 5 Years ago, written in Perl.
This paste is a reply to Re: Untitled from Soft Terrapin - view diff
Embed
Download Paste or View Raw
Hits: 301
  1. # Create helper
  2. $app->helper(category_select => sub {
  3.   my ($self, $id) = @_;
  4.  
  5.   my $categories = $self->app->db->resultset('Category');
  6.  
  7.   my $options = Mojo::Collection->new(['*' => 0]);
  8.   while (my $category = $categories->next) {
  9.     my @attr;
  10.     push @attr, selected => 'selected' if $category->id == $id;
  11.     push @{$options}, [$category->name, $category->id, @attr];
  12.   }
  13.  
  14.   return $options;
  15. });
  16.  
  17. # _form.html.ep
  18. <td>
  19.   % my $options = category_select $category->id;
  20.   %= select_field parent_id => $options => (class=>'input')
  21. </td>
  22.