Skip to content

Instantly share code, notes, and snippets.

@kfly8
Created February 28, 2015 07:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kfly8/c25c9ecf60aa67d65242 to your computer and use it in GitHub Desktop.
Save kfly8/c25c9ecf60aa67d65242 to your computer and use it in GitHub Desktop.
Perl入学式 #6 の掲示板の練習問題で、DBに記事を格納する形にしてみる
#!/usr/bin/env perl
use Mojolicious::Lite;
use DBIx::Sunny;
app->attr(dbh => sub { DBIx::Sunny->connect('dbi:mysql:dbname=bbs', 'root','') });
get '/' => sub {
my $c = shift;
my $entries = app->dbh->select_all('SELECT * FROM entry');
$c->stash(entries => $entries);
$c->render(template => 'index');
};
post '/entry' => sub {
my $c = shift;
my $body = $c->param('body');
app->dbh->query('INSERT INTO entry (body) VALUES (?)', [$body]);
$c->redirect_to('/');
};
app->start;
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Bbs';
%= form_for '/entry', method => 'POST', begin
%= text_field 'body'
%= submit_button '投稿する'
% end
% for my $entry (@$entries) {
<pre>
<%= $entry->{body} %>
</pre>
% }
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>
CREATE DATABASE `bbs`;
CREATE TABLE `entry` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`body` text,
PRIMARY KEY (`id`)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment