На моей веб-странице у меня есть записи, которые пользователи могут редактировать. Когда они редактируют. Они нажимают кнопку "редактировать" рядом с соответствующей записью, которая выводит их на страницу редактирования. Здесь они могут редактировать разные поля, а затем нажать кнопку "SUBMIT", чтобы отправить отредактированный пост. Они нажимают "Отправить". Ничего не произошло. Они снова нажимают кнопку "Подтвердить", после чего изменения сохраняются и они перенаправляются обратно в индекс. Это происходит для каждой отдельной записи.
Вот мой контроллер для редактирования.
function edit ($id = NULL) {
$this->loadModel('RecordDrugUnit');
$this->loadModel('Drug');
$this->loadModel('Unit');
$this->loadModel('Route');
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$record=$this->RecordDrugUnit->findByrecordId($id);
if(!$record) {
throw new NotFoundException(__('Invalid post'));
}
/*----
* Divide dose by
* associated conversion
-----*/
$record['RecordDrugUnit']['dose'] = $record['RecordDrugUnit']['dose'] / $record['Unit']['conversion'];
if($this->request->is('post')) {
$this->request->data['Record']['id'] = $id;
$this->request->data['RecordDrugUnit']['record_id'] = $id;
$this->request->data['RecordDrugUnit']['id'] = $record['RecordDrugUnit']['id'];
$this->request->data['Record']['user_id'] = $this->Auth->user('id');
/*----
* Multiply dose by
* associated conversion
-----*/
$conv_val = $this->Unit->find('first',
array(
'conditions' => array(
'id' => $this->request->data['RecordDrugUnit']['unit_id']),
'fields' => array('conversion')
));
$this->request->data['RecordDrugUnit']['dose'] = (float)$this->request->data['RecordDrugUnit']['dose'] * (float)$conv_val['Unit']['conversion'];
if ($this->RecordDrugUnit->saveAssociated($this->request->data, array('deep' => TRUE))) {
$this->Session->setFlash('Your log has been updated.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to update your post.');
}
}
if(!$this->request->data) {
$this->request->data = $record;
}
}
И вид
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker({ dateFormat: 'yy-m-dd' });
});
</script>
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('Record'); ?>
<header id='topHead'>
<div class="frame headWrap">
<div class="bit-2">
<div class ='padding background_color'>
<?php
echo $this->Form->input('Record.dose_date', array('placeholder' => 'Date of Dose', 'label' => false,'type' => 'text','id' =>'datepicker'));
echo $this->Form->input('RecordDrugUnit.drug_id', array('placeholder' => 'Substance', 'options'=>$drugList,'label' => false,'type' => 'select'));
echo $this->Form->input('RecordDrugUnit.dose', array('placeholder' => 'Dose', 'label' => false, 'class' => 'colLarge left'));
echo $this->Form->input('RecordDrugUnit.unit_id', array('placeholder'=>'Unit', 'options'=>$unitList, 'label' => false,'type'=>'select', 'class' => 'colSmall right'));
echo $this->Form->submit('EDIT', array('class' => 'button'));
?>
</div>
</div>
<div class="bit-2">
<div class ='padding background_color'>
<span class='smallSuper'>these fields are optional</span>
<br>
<span class='smallx colSmall left'>ROA: </span>
<?php
echo $this->Form->input('RecordDrugUnit.route_id', array('placeholder'=>'Unit', 'options'=>$routeList, 'label' => false,'type'=>'select', 'class' => 'colLarge right'));
echo $this->Form->input('Record.title', array('placeholder' => 'Title', 'label' => false,'type' => 'text'));
echo $this->Form->input('Record.report', array('placeholder' => 'Your Report','label' => false,'type' => 'textarea'));
?>
</div>
</div>
</div>
</header>
<?php echo $this->Form->end(); ?>
Нашел решение этой проблемы.
Ошибка заключалась в том, что мой контроллер редактирования отправлял запрос на отправку, а мой код проверял только почтовый запрос.
Изменить строку
if($this->request->is('post')) {
в
if($this->request->is('post') || $this->request->is('put')) {