cakephp 2,通过bootstrap编辑模态记录

时间:2015-09-22 08:20:56

标签: cakephp modal-dialog

如何使用Cakephp和模态引导程序编辑记录?

因为当我编辑联系人时,我收到错误500?

  

缺少视图UserContactsController的视图:: admin_modal_edit()   没找到。

在控制器admin_modal_edit()中我设置了$ this-> layout = NULL;

这些是应用程序的文件。

文件:user / view.ctp,包含联系人列表和添加或编辑的模式。

编辑或删除按钮



<?php echo $userContact['UserContactType']['title']; ?>: <?php echo $userContact['contact']; ?>

<a href="<?php echo $this->Html->url(array('controller' => 'user_contacts', 'action' => 'modal_edit', $userContact['id'] )); ?>" class="btn btn-default btn-xs"  data-toggle="modal" data-target="#myModal"><?php echo __('Edit'); ?></a>
&#13;
&#13;
&#13;

BOOTSTRAP MODAL

&#13;
&#13;
<?php
echo $this->Form->create('UserContact', array('url' => array('admin' => true, 'prefix' => 'admin', 'plugin' => 'user', 'controller' => 'user_contacts', 'action' => 'modal_edit')));
?>

<?php echo $this->Form->input('UserContact.id', array('class' => 'form-control')); ?>

<?php echo $this->Form->input('UserContact.user_id', array('default' => $user_id, 'type' => 'hidden')); ?>

<?php echo $this->Form->input('UserContact.user_contact_type_id', array('class' => 'form-control', 'empty' => true)); ?>

<?php echo $this->Form->input('UserContact.contact', array('class' => 'form-control')); ?>

<?php echo $this->Form->submit(__('Save'), array('div' => false, 'class' => 'btn btn-success')); ?>

<?php echo $this->Form->end(); ?>
&#13;
&#13;
&#13;

文件:controller / UsersController.php,生成view.ctp

public function admin_view($id = null) {
    if (!$this->User->exists($id)) {
        throw new NotFoundException(__('Invalid user'));
    }
    $user_id = $id;
    $options = array(
        'contain' => array('UserContact' => 'UserContactType', 'UserGroup', 'UserState', 'UserGender', 'UserAddress' => 'UserAddressType', 'UserPaymentType', 'Item', 'Comment'),
        'conditions' => array('User.' . $this->User->primaryKey => $id));
    $this->set('user', $this->User->find('first', $options));

    // blocco find list
    $userContactTypes = $this->UserContactType->find('list');
    $userAddressTypes = $this->UserAddressType->find('list');
$this->set(compact(array('userContactTypes', 'userAddressTypes', 'user_id')));
}

文件:控制器/ UserContactsController.php用于模态

public function admin_modal_edit() {
    $id =  $this->request->query('id');
    $this->layout = NULL;
    if ($this->request->is('post') || $this->request->is('put')) {

        if ($this->UserContact->save($this->request->data)) {
            $this->Session->setFlash(__('The record has been saved'), 'flash/success');
            $this->redirect(array('controller' => 'users', 'action' => 'view', $this->request->data['UserContact']['user_id']));
        } else {
            $this->Session->setFlash(__('The record could not be saved. Please, try again.'), 'flash/error');
        }
    } else {
        if (!empty($id)) {
            $options = array('conditions' => array("UserContact.{$this->UserContact->primaryKey}" => $id));
            $this->request->data = $this->UserContact->find('first', $options);

        }
    }

}

2 个答案:

答案 0 :(得分:0)

你的问题是CakePHP试图渲染&#39; admin_modal_edit.ctp&#39;查看模板。

如果您不希望CakePHP渲染任何设置autoRender to false: -

public function admin_modal_edit() {
    $this->autoRender = false;
}

这将阻止CakePHP查找要呈现的视图模板。

$this->layout = null不会阻止Cake尝试呈现模板。我怀疑你正在努力实现的目标。

答案 1 :(得分:0)

此错误显示,因为cakephp的抱怨函数admin_modal_edit对于不具有图,因为它不能找到admin_modal_edit.ctp在控制器的视图文件夹中。

要解决此问题,请添加此

  

$this->autoRender = false

到函数admin_modal_edit以禁用该函数的视图呈现。

所以你的功能应该是这样的。

public function admin_modal_edit() {
    /** --------------   **/
    $this->autoRender = false;
    /** --------------  **/

    $id =  $this->request->query('id');
    if ($this->request->is('post') || $this->request->is('put')) {
        if ($this->UserContact->save($this->request->data)) {
            $this->Session->setFlash(__('The record has been saved'), 'flash/success');
            $this->redirect(array('controller' => 'users', 'action' => 'view', $this->request->data['UserContact']['user_id']));
        } else {
            $this->Session->setFlash(__('The record could not be saved. Please, try again.'), 'flash/error');
        }
    } else {
        if (!empty($id)) {
            $options = array('conditions' => array("UserContact.{$this->UserContact->primaryKey}" => $id));
            $this->request->data = $this->UserContact->find('first', $options);
        }
   }
}