I’ve spent a bunch of time trying to figure this problem out and had zero luck. I have tried routing with no success. I’ll put the logic first then the code.
- Users are in /pra/Fields/view/1 looking at a list of fields where fields.template_id=1
- Users click add new field /pra/Fields/add_new/1 they are brought to a form where they enter the information they want to
about the new field they create.- when a user clicks on add/submit, the new field is saved to the database
- The user is taken back to /pra/Fields/view/1 so they can see the new field added to the template
currently the first 3 steps are happening, however when it comes to redirecting back to /pra/Fields/view/1 The user is being redirected to /pra/Fields/view/
here is the code for the add_new function
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);
here is the add_new view
<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>
and here is the view function code
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));
}
It might happen that you have an issue here:
Check if $name is not empty
the rest of your code