문제

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.

도움이 되었습니까?

해결책

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'); ?>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top