Skip to content

Instantly share code, notes, and snippets.

@hayajo
Created October 2, 2013 03:03
Show Gist options
  • Save hayajo/6788559 to your computer and use it in GitHub Desktop.
Save hayajo/6788559 to your computer and use it in GitHub Desktop.
Mojoliciousで確認画面をともなう画面遷移の実装例
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/' => sub {} => 'index';
post '/' => sub {
my $self = shift;
# is valid?
if ( $self->param('text') eq 'hoge' ) {
# from confirming?
if ( $self->param('confirming') ) {
$self->flash(success => 1);
$self->redirect_to('index');
}
else {
$self->stash(confirming => 1);
}
}
else {
$self->res->code(422);
$self->stash(confirming => 0);
}
} => 'index';
app->start;
__DATA__
@@ index.html.ep
% layout 'default';
% title 'FillInForm';
% if ($self->flash('success')) {
%= t p => '登録しました'
% }
%= form_for index => (method => 'POST') => begin
% if (!$self->stash('confirming')) {
%= label_for text => 'テキスト'
%= text_field 'text'
<br>
%= label_for radio => 'ラジオボタン'
%= radio_button radio => 'male', id => 'radio_male'
%= label_for radio_male => '男'
%= radio_button radio => 'female', id => 'radio_female'
%= label_for radio_female => '女'
<br>
%= label_for select => 'セレクト'
%= select_field select => [[ 'ほげ' => 'hoge' ], [ 'ふが' => 'fuga' ], [ 'ぴよ' => 'piyo' ]]
<br>
%= submit_button
% } else {
%= label_for text => 'テキスト'
%= t p => $self->param('text')
%= hidden_field 'text', $self->param('text')
<br>
%= label_for radio => 'ラジオボタン'
%= t p => ($self->param('radio') eq 'male') ? '男' : '女'
%= hidden_field 'radio', $self->param('radio')
<br>
%= label_for select => 'セレクト'
%= t p => ($self->param('select') eq 'hoge') ? 'ほげ' : ($self->param('select') eq 'fuga') ? 'ふが' : 'ぴよ'
%= hidden_field 'select', $self->param('select')
<br>
%= submit_button
% }
%= hidden_field confirming => $self->stash('confirming') || 0;
% end
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>
@hayajo
Copy link
Author

hayajo commented Oct 2, 2013

弟子くん向けに

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment