Created
February 28, 2015 07:06
-
-
Save kfly8/c25c9ecf60aa67d65242 to your computer and use it in GitHub Desktop.
Perl入学式 #6 の掲示板の練習問題で、DBに記事を格納する形にしてみる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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