Index page is blank after new record entry

Greetings all,

I have an "add comments" page and when I try to add a new entry for my page's comments, I am returned to the main page (which is what I want), but the main page is blank!? I then have to refresh the page (usually I can only do it by pressing "Go" to load the page again) to get the information to show. The comment is added, but for some reason the page is blank upon the new entry submission. Here is the code for the index page:



<h1>Comments</h1> 
  <?php echo $html->link('Add Comment',array('controller' => 'comments', 'action' => 'add')); ?>

<table> 
   <tr> 
       <th>ID</th> 
       <th>Name</th>  
       <th>Date</th>
   </tr> 
   <?php foreach ($comments as $comment): ?> 
   <tr> 
       <td><?php echo $comment['Comment']['id']; ?></td> 
       <td> 
           <?php echo $html->link($comment['Comment']['name'], "/comments/view/".$comment['Comment']['id'])?>   
     [<?php echo $html->link('Edit', array('action' => 'edit', 'id' => $comment['Comment']['id']));?> |  
     <?php echo $html->link('Delete', "/comments/delete/".$comment['Comment']['id'], null, 'Are you sure?')?>]  
       </td>
       <td><?php echo $comment['Comment']['com_date']; ?></td> 
 </tr> 
   <?php endforeach; ?> 
</table>

and this is the code for the add page:


<h1>Add Comment</h1> 

   <?php
   echo $form->create('Comment');
   echo $form->input('name'); //text
   echo $form->input('com_date');
   echo $form->input('comment', array('rows' => '3'));
   echo $form->end('Add Information');
   ?>

any suggestions?

Asked by bbender, on 11/11/09

2 Answers

In your controller add() method, after you process the save, you need to redirect like below:


public function add() {
  if (!empty($this->data)) {
    $this->Comment->create($this->data);
    if ($this->Comment->save()) {
      $this->Session->setFlash(__('Comment was saved!'));
      return $this->redirect(array('action' => 'index'));
    } else {
      $this->Session->setFlash(__('An error occurred saving your comment'));
    }
  }
}

This will ensure the user is directed somewhere useful after the save/add operation is processed.

The controller code you have might be useful to determine what the problem is. If adjusting your code to match something like what I have above doesn't work, paste us your controller code.

Answered by predominanton 12/11/09

Thank you thank you! Works perfectly

bbender - on 12/11/09

<< comments | comments >>

If it's blank, then most likely a fatal error happened.

Have you turned on debugging?(Configure::write('debug', 2)) and checked your apache logs?

Probably a mistake in the controller Add method.

Answered by Crazyon 11/11/09

@bebender

The correct way is that you have to check if the del() call in your delete action was successful, and then redirect back to any page you want (usually the index page of the same model).

ojtibi - on 12/11/09

<< comments | 1 | 2 | 3
<< previous next >>

Tagged with

Rating

1

Viewed

620 times

Last Activity

on 12/11/09