我想编辑我的帖子,但是当我访问它时http:// ... / posts / edit / 2它只是显示帖子已经更新的Flash消息,怎么回事?它没有显示编辑表单......
function edit($id = NULL) {
$this->Post->id = $id;
if($this->request->is('post')){
$this->request->data = $this->Post->read();
}else {
if($this->Post->save($this->request->data)){
$this->Session->setFlash('The post has been updated');
$this->redirect(array('action'=>'index'));
}
}
}
我的编辑页面
<h2>Edit post</h2>
<?php
echo $this->Form->create('post',array('action'=>'edit'));
echo $this->Form->input('title');
echo $this->Form->input('body');
echo $this->Form->input('id', array('type'=>'hidden'));
echo $this->Form->end('Edit Post');
?>
答案 0 :(得分:1)
您的if()条件错误:
因此,当您访问表单时,不会发布任何数据,因此保存,然后重定向。修复方法是修改if()中的条件,以便在发布时不发布和保存:
if(!$this->request->is('post'))
答案 1 :(得分:0)
您编辑功能应如下所示。
function edit($id = NULL) {
$this->Post->id = $id;
if($this->request->is('post')){
if($this->Post->save($this->request->data)){
$this->Session->setFlash('The post has been updated');
$this->redirect(array('action'=>'index'));
}
}else{
$this->request->data = $this->Post->read();
}
}