我有一個Horse
對象,它具有completed
變量(文本)。我希望能夠在視圖中點擊set complete
,然後將該馬的completed
變量設置爲「是」。在此之後,我們會將用戶重定向到任務/索引。我的想法是在控制器中創建setcomplete($id)
函數,所以我可以通過/setcomplete/4
,那個馬的旗子會改變。更改AppController中的單個變量值
我已經爲它創建了一個基本視圖,但想法是我不需要視圖,但我不知道如何解決這個問題....我想使用$this->render()
或者這種性質的東西。
這是基本代碼...傳入id然後更改變量,然後重定向到tasks/index。
我沒有收到錯誤....它只是不工作,就這樣。
public function setcomplete($id = null) {
if (!$this->Task->exists($id)) {
throw new NotFoundException(__('Invalid task'));
}
if ($this->request->is(array('post', 'put'))) {
$this->set('completed', 'yes');
if ($this->Task->save($this->request->data)) {
$this->Session->setFlash(__('The task has been updated.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The task could not be saved. Please, try again.'));
}
} else {
}
}
你看到了什麼行爲?但是'$ this-> layout = $ this-> autoRender = false;'將阻止它拋出關於缺少視圖的錯誤。 – Kai
即時看到沒有行爲...我設置了一個非常基本的視圖,它去那裏,但它並沒有改變變量,這是我想要它做的。 –
您可以在執行保存的行上添加'debug($ this-> request-> data)'並在此處發佈結果嗎?可以幫助找出問題。 –