0

There is a problem when I write add() function for UsersController.

    public function add(){
        if ($this->request->is('post')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash('The new user has been saved.');
                $this->redirect(array('action' => 'test'));
            }
        }
        $this->set('title_for_layout', 'Register');
    }

This is add view ctp.

    <?php
    echo $this->Form->create('User');
    echo $this->Form->input('username');
    echo $this->Form->input('password');
    echo $this->Form->end('Save User');
    ?>

There's always a internal error when I try to access to users/add. Anyone know how to deal with this problem? Thanks.

horatio.mars
  • 559
  • 3
  • 9
  • 17
  • When it says "Internal Error", that means that debug is set to 0. If you set debug to 2, in core.php, you will find a more descriptive error message. – Nick Dec 09 '11 at 20:23
  • Thanks very much. It should be a help. I have already figured this problem out. I just downloaded the latest edition of Cakephp and followed the steps in this tutorial. Very nice. http://www.youtube.com/user/andrewperk#p/u/0/zvwQGZ1BxdM – horatio.mars Dec 09 '11 at 23:00

1 Answers1

1

Have you tried testing for $this->data instead of $this->request->is('post')? It might not matter, but that's typically the way it is done.

Also, for saving, you should most likely (unless you are setting userid manually) do something like:

$this->User->create();
$this->User->save($this->data);

So your add function should look something like:

public function add(){
        if ($this->data) {
            $this->User->create();
            if ($this->User->save($this->data)) {
                $this->Session->setFlash('The new user has been saved.');
                $this->redirect(array('action' => 'test'));
            }
        }
        $this->set('title_for_layout', 'Register');
    }

And you probably want your view to be something like:

<?php echo $form->create('User', array('action' => 'add')); ?>
<?php echo $form->input("username", array('label' => 'Username'))   ?>
<?php echo $form->input("password",array("type"=>"password", 'label' => 'password')) ?>
<?php echo $form->submit('Submit'); ?>
swiecki
  • 3,483
  • 3
  • 23
  • 19
  • I tried your method, it doesn't work either. I followed the tutorial and tried login function, the same problem exists. Here is the code. Form->create(); echo 'testcreate'; echo $this->Form->input('username'); echo $this->Form->input('password'); echo $this->Form->end('Login'); ?> – horatio.mars Dec 09 '11 at 19:53
  • I have already figured this problem out by updating to the latest cakephp edition. Thanks very much. – horatio.mars Dec 09 '11 at 23:03