我花了很多时间试图解决这个问题并且运气为零。我试过路由没有成功。我会把逻辑放在第一位,然后是代码。
- 用户在 /pra/Fields/view/1 中查看 fields.template_id=1 的字段列表
- 用户单击添加新字段/pra/Fields/add_new/1,他们将进入一个表单,在该表单中输入他们想要的关于他们创建的新字段的信息。
- 当用户单击添加/提交时,新字段将保存到数据库中
- 用户被带回 /pra/Fields/view/1,因此他们可以看到添加到模板中的新字段
目前前 3 个步骤正在发生,但是当涉及到重定向回/pra/Fields/view/1
用户被重定向到/pra/Fields/view/
这是 add_new 函数的代码
function add_new($id=null){
//allows users to add another field to an existing template
$this->set('title_for_layout', 'Create Fields');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
$name=$this->Field->field('template_id', array('template_id'=>$id));
//if the field data saves
$this->set('name',$name);
if(($this->Field->save($this->data)))
{
//user is redirected to fields/view/$name
$this->redirect(array('controller'=>'Fields', 'action'=>'view',$name));
}
//sets the $id variable
$this->set('id',$id);
这是 add_new 视图
<table id="formatform">
</br></br>
<?php
$options = array('Money'=>'Money','Text'=>'Text','Description'=>'Description');
?>
<?php echo $this->Form->create('Field', array('action'=>'add_new')); ?>
<?php echo $this->Form->input('id',array('type'=>'hidden')); ?>
<tr>
<td></td>
<td><?php echo $this->Form->input('template_id',array('type'=>'hidden','default' => $id)); ?></td>
</tr>
<tr>
<td align='center'>Field Name:</td>
<td align='left'><?php echo $this->Form->input('name', array('label'=>false)); ?></td>
</tr>
<tr>
<td align='center'>Default Value:</td>
<td align='left'><?php echo $this->Form->input('default_value', array('label'=>false)); ?></td>
</tr>
<tr>
<td align='center'>Field Type:</td>
<td align='left'><?php echo $this->Form->input('field_type', array('label'=>false,'type'=>'select','options'=>$options)); ?></td>
</tr>
<tr>
<td align='center'>Description:</td>
<td align='left'><?php echo $this->Form->input('description', array('label'=>false)); ?></td>
<td><?php echo $this->Form->input('active', array('type'=>'hidden','default'=>true)); ?></td>
</tr>
<td></td> <td align = "left"><?php echo $this->Form->end('Submit');?></td>
这是查看功能代码
function view($name){
//lists information about fields that correspond to the
//template they have clicked 'view' on
$this->set('title_for_layout', 'Field Details');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
//sets $conditions where template_id=$name and field.active=true
$conditions=array(
"AND"=>array(
'template_id'=> $name,
'Field.active'=>true));
//finds all fields where 'conditions'=$conditions
$fields = $this->Template->Field->find('all',array(
'conditions' => $conditions));
//sets all the variables
$this->set('conditions', $conditions);
$this->set('field', $fields);
$this->set('field', $this->paginate('Field', $conditions));
}