2010-10-08 71 views
0

來自絕對n00b的簡單問題。CakePHP:編輯屏幕中沒有填充的字段

我正在嘗試基於Cake網站上的博客教程中給出的代碼創建一個編輯內容屏幕。

這裏的控制器(它的命名內容)我的編輯功能:

function edit($id = null) { 
    $this->Content->id = $id; 
    Debugger::dump($this->Content->id); 
    if(empty($this->data)) { 
     $this->data = $this->Content->read(); 
     Debugger::dump($this->Content); 
    } 
    else { 
     if($this->Content->save($this->data)) { 
     $this->Session->setFlash('Content has been updated.'); 
     $this->redirect(array('action' => 'index')); 
     } 
    } 
} 

這是edit.ctp

echo $form->create('Content', array('action' => 'edit')); 
echo $form->input('id', array('type' => 'hidden')); 
echo $form->input('title'); 
echo $form->input('nicename'); 
echo $form->end('Save'); 

然而,當我參觀編輯屏,輸入字段不會與請求的數據一起獲得信息。

我使用HTML輔助生成的鏈接和我的編輯鏈接的格式如下:

http://localhost/contents/edit/id:4c8efaa0-02fc-46bf-ac65-06a07614f57d 

正如你所看到的,我推出了兩種調試器在控制器的編輯功能轉儲...那些顯示$ this-> Content-> id在我訪問此URL時保持爲NULL。

但是,如果我修改URL以這種形式(請注意,我用'='代替':'):

http://localhost/contents/edit/id=4c8efaa0-02fc-46bf-ac65-06a07614f57d 

調試報告$這個 - >內容 - >編號以具有正確的值...

但是,我的領域仍然沒有得到填充。

索引功能工作正常,但...

function index() { 
    $this->set('contents', $this->Content->find('all')); 
} 

上述功能是否正確列出的內容!

我哪裏錯了?我是否錯過了View中的一些重要代碼? 任何幫助將不勝感激。

感謝, 平方公尺Ë

回答

2

的問題是在這裏:

http://localhost/contents/edit/id:4c8efaa0-02fc-46bf-ac65-06a07614f57d 

基本上您的網址應該是這樣的:

http://localhost/contents/edit/4c8efaa0-02fc-46bf-ac65-06a07614f57d 

正如你可能會注意到ID:從失蹤正確的網址。原因是:如果你傳遞了id:這是命名參數,它沒有被分配給函數中的$ id。

您的鏈接編輯表單應該是這樣的:

$this->Html->link('Edit', array('controller'=>contents', 'action'=>'edit', $id_variable)); 
+0

謝謝聶。在等待答覆時,我決定完全重新編寫代碼並重新開始......並且通過檢查腳手架生成的鏈接,我得出了與您的結論相同的結論。 – 2010-10-08 15:56:26

+0

嗯..仍然有鏈接生成問題..這裏是我的代碼:echo $ html-> link('Edit',array('controller'=>'contents','action'=>'edit','id'=> $ item ['Content'] ['id'])); – 2010-10-08 16:00:52

+0

然後......沒關係..想通了。它應該是:echo $ html-> link('Edit',array('controller'=>'contents','action'=>'edit',$ item ['Content'] ['id'])) – 2010-10-08 16:02:55