10.1.11 投稿記事の削除
The original text for this section has changed since it was translated. Please help resolve this difference. You can:
次に、ユーザが投稿記事を削除できるようにする機能を作りましょう。PostsControllerのdelete()アクションを作るところから始めます。
function delete($id) {
$this->Post->del($id);
$this->flash('The post with id: '.$id.' has been deleted.', '/posts');
}
function delete($id) {$this->Post->del($id);$this->flash('The post with id: '.$id.' has been deleted.', '/posts');}
このロジックは、$idで指定された記事を削除し、flash()を使って、ユーザに確認メッセージを表示し、それから /posts にリダイレクトします。
ロジックを実行してリダイレクトするので、このアクションにはビューがありません。しかし、indexビューにリンクを付けて、投稿を削除するようにできるでしょう。
/app/views/posts/index.ctp
<h1>Blog posts</h1>
<p><?php echo $html->link('Add Post', '/posts/add'); ?></p>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Actions</th>
<th>Created</th>
</tr>
<!-- ここで$posts配列をループして、投稿情報を表示 -->
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $html->link($post['Post']['title'], '/posts/view/'.$post['Post']['id']);?>
</td>
<td>
<?php echo $html->link('Delete', "/posts/delete/{$post['Post']['id']}", null, 'Are you sure?' )?>
</td>
<td><?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>
</table> /app/views/posts/index.ctp<h1>Blog posts</h1><p><?php echo $html->link('Add Post', '/posts/add'); ?></p><table><tr><th>Id</th><th>Title</th><th>Actions</th><th>Created</th></tr><!-- ここで$posts配列をループして、投稿情報を表示 --><?php foreach ($posts as $post): ?><tr><td><?php echo $post['Post']['id']; ?></td><td><?php echo $html->link($post['Post']['title'], '/posts/view/'.$post['Post']['id']);?></td><td><?php echo $html->link('Delete', "/posts/delete/{$post['Post']['id']}", null, 'Are you sure?' )?></td><td><?php echo $post['Post']['created']; ?></td></tr><?php endforeach; ?></table>
注意: このビューコードはHtmlHelperを使い、削除する前に、JavaScriptによる確認ダイアログでユーザに確認します。
