Create An Action And A View!

Hi Yii!! I use a [color="#00BFFF"]"_comment_form.php"[/color] in the [color="#9ACD32"]"actionView"[/color] of [color="#00BFFF"]"ProjectController.php"[/color] for the comments of the users.

[color="#9ACD32"]"actionView"[/color]




public function actionView($id)

	{

                $project=$this->loadModel($id);

		$comment=$this->actionComment_form($project);

                $this->render('view',array(

			'model'=>$project,

			'comments'=>$comment,

                ));

	}



[color="#00BFFF"]"_comment_form.php"[/color]




<?php /** @var BootActiveForm $form */

    $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(

    'id'=>'comments-_comment_form-form',

    'htmlOptions'=>array('class'=>'well'),

    'type'=>'horizontal',

)); ?>

    <fieldset>

        <legend>Send a comment</legend>

        <p>Fields with <span class="required">*</span> are required.</p>

        <?php echo $form->errorSummary($model); ?>

        <?php echo $form->textFieldRow($model, 'title', array('class'=>'span3')); ?>

        <?php echo $form->textAreaRow($model, 'content', array('class'=>'span3', 'rows'=>3)); ?>

    </fieldset>


    <?php $form->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'type'=>'primary', 'label'=>'Comment')); ?>

<?php $this->endWidget(); ?>



[color="#9ACD32"]"actionComment_form"[/color]




public function actionComment_form($project)

        {

            $comment=new Comments;


            if(isset($_POST['Comments']))

            {

                $comment->attributes=$_POST['Comments'];

                $comment->comment_project=$project->id;

                $comment->comment_user=Yii::app()->user->data()->id;

                if($project->addComment($comment))

                {

                    Yii::app()->user->setFlash('success', '<strong>Sended!</strong> Thanks.');

                    $this->refresh();

		} else {

                    Yii::app()->user->setFlash('error', '<strong>Oh snap!</strong> Change a few things up and try submitting again.');

                    $this->refresh();

                }

            }

            

            return $comment;

        }



[color="#00BFFF"]"view.php"[/color]




<?php

$this->breadcrumbs=array(

	'Projects'=>array('index'),

	$model->title,

);


$this->menu=array(

	array('label'=>'List Projects', 'url'=>array('index')),

	array('label'=>'Create Projects', 'url'=>array('create')),

	array('label'=>'Update Projects', 'url'=>array('update', 'id'=>$model->id)),

	array('label'=>'Delete Projects', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),

	array('label'=>'Manage Projects', 'url'=>array('admin')),

);


?>


<h1>View Projects (<?php echo $model->title; ?>)</h1>

<?php

    $this->widget('bootstrap.widgets.TbDetailView', array(

        'data'=>$model,

        'type' => 'striped',

        'attributes'=>array(

            array('name'=>'id'),

            array('name'=>'title'),

            array('name'=>'description'),

            array('name'=>'date'),

            array('name'=>'project_user'),

        ),

    ));

?>

<?php if(Yii::app()->user->hasFlash('success')||Yii::app()->user->hasFlash('error')):?>

    <?php $this->widget('bootstrap.widgets.TbAlert', array(

        'block'=>true, // display a larger alert block?

        'fade'=>true, // use transitions?

        'closeText'=>'&times;', // close link text - if set to false, no close link is displayed

        'alerts'=>array( // configurations per alert type

            'success'=>array('block'=>true, 'fade'=>true, 'closeText'=>'&times;'), // success, info, warning, error or danger

            'error'=>array('block'=>true, 'fade'=>true, 'closeText'=>'&times;'), // success, info, warning, error or danger

        ),));

    ?>

<?php endif; ?>

<div id="project_comment">

    <?php

        $comment=new Comments;

        echo $this->renderPartial('_comments', array('model'=>$model));

        echo $this->renderPartial('_comment_form', array('model'=>$comment));

    ?>

	

</div>

<div style="clear: both;"> </div>




And it works. Also, the projects [color="#00FF00"]HAS_MANY[/color] "Tags", so i tried with the same way, [color="#00BFFF"]"_tags_form.php"[/color] in the [color="#9ACD32"]"actionView"[/color] of [color="#00BFFF"]"ProjectController.php"[/color], and it works too, the [color="#00BFFF"]"_comment_form.php"[/color] and the [color="#00BFFF"]"_tags_form.php"[/color].

[color="#9ACD32"]"actionView"[/color]




public function actionView($id)

	{

                $project=$this->loadModel($id);

		$comment=$this->actionComment_form($project);

                $tag=$this->actionTag_form($project);

                $this->render('view',array(

			'model'=>$project,

			'comments'=>$comment,

                        'tag'=>$tag,

                ));

	}



[color="#00BFFF"]"_tag_form.php"[/color]




<?php /** @var BootActiveForm $form */

    $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(

    'id'=>'tags-_tags_form-form',

    'htmlOptions'=>array('class'=>'well'),

    'type'=>'horizontal',

)); ?>

    <fieldset>

        <legend>Assign a tag</legend>

        <?php echo $form->errorSummary($model); ?>

        <?php echo $form->textFieldRow($model, 'tag', array('class'=>'span3')); ?>

        <?php $form->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'type'=>'primary', 'label'=>'Send')); ?>

    </fieldset>

<?php $this->endWidget(); ?>



[color="#9ACD32"]"actionComment_form"[/color]




 public function actionTag_form($project)

        {

            $tag=new Tags;

            if(isset($_POST['Tags']))

            {

                $tag->attributes=$_POST['Tags'];

                $tag->attributes=$tags->attributes;

                $tag->tag_project=$project->id;

                if(!$project->addTag($tag))

                {

                    Yii::app()->user->setFlash('success', '<strong>Sended!</strong> Thanks.');

                    $this->refresh();

		} else {

                    Yii::app()->user->setFlash('error', '<strong>Oh snap!</strong> Change a few things up and try submitting again.');

                    $this->refresh();

                }

            }

            

            return $tag;

        }



[color="#00BFFF"]"view.php"[/color]




<?php

$this->breadcrumbs=array(

	'Projects'=>array('index'),

	$model->title,

);


$this->menu=array(

	array('label'=>'List Projects', 'url'=>array('index')),

	array('label'=>'Create Projects', 'url'=>array('create')),

	array('label'=>'Update Projects', 'url'=>array('update', 'id'=>$model->id)),

	array('label'=>'Delete Projects', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),

	array('label'=>'Manage Projects', 'url'=>array('admin')),

        array('label'=>'Add Tags', 'url'=>array('addTag', 'id'=>$model->id)), // New Menu item

);


?>


<h1>View Projects (<?php echo $model->title; ?>)</h1>

<?php

    $this->widget('bootstrap.widgets.TbDetailView', array(

        'data'=>$model,

        'type' => 'striped',

        'attributes'=>array(

            array('name'=>'id'),

            array('name'=>'title'),

            array('name'=>'description'),

            array('name'=>'date'),

            array('name'=>'project_user'),

        ),

    ));

?>

<?php if(Yii::app()->user->hasFlash('success')||Yii::app()->user->hasFlash('error')):?>

    <?php $this->widget('bootstrap.widgets.TbAlert', array(

        'block'=>true, // display a larger alert block?

        'fade'=>true, // use transitions?

        'closeText'=>'&times;', // close link text - if set to false, no close link is displayed

        'alerts'=>array( // configurations per alert type

            'success'=>array('block'=>true, 'fade'=>true, 'closeText'=>'&times;'), // success, info, warning, error or danger

            'error'=>array('block'=>true, 'fade'=>true, 'closeText'=>'&times;'), // success, info, warning, error or danger

        ),));

    ?>

<?php endif; ?>

<?php

    $tag=new Tags;

    echo $this->renderPartial('_tags', array('model'=>$model));

    echo $this->renderPartial('_tag_form', array('model'=>$tag));

?>

<div id="project_comment">

    <?php

        $comment=new Comments;

        echo $this->renderPartial('_comments', array('model'=>$model));

        echo $this->renderPartial('_comment_form', array('model'=>$comment));

    ?>

	

</div>

<div style="clear: both;"> </div>




But what i really need is to show the [color="#00BFFF"]"_comment_form.php"[/color] and the [color="#00BFFF"]"_tags_form.php"[/color] in separated views.

One way is to show the [color="#00BFFF"]"_tags_form.php"[/color] inside [color="#00BFFF"]"_form.php"[/color], i tried but the [color="#00BFFF"]"_tags_form.php"[/color] doesn’t save.

"[color="#00BFFF"]_form.php[/color]"





<?php /** @var BootActiveForm $form */

        $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(

            'id'=>'projects-form',

            'htmlOptions'=>array('class'=>'well'),

            'type'=>'horizontal',

        )); 

    ?>


    <fieldset>

        <p>Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>

        

        <?php echo $form->textFieldRow($model, 'title', array('class'=>'span3')); ?>

        <?php echo $form->textAreaRow($model, 'description', array('class'=>'span3', 'rows'=>3)); ?>

        <?php

            $tags=new Tags;

            echo $this->renderPartial('_tags', array('model'=>$model));

            echo $this->renderPartial('_tag_form', array('model'=>$tags));

            echo $this->renderPartial('_tags_admin', array('model'=>$model));

        ?>

    <?php $form->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'type'=>'primary', 'label'=>($model->isNewRecord ? 'Create' : 'Save'))); ?>


<?php $this->endWidget(); ?>



So i thought embbed the [color="#00BFFF"]"_tags_form.php"[/color] in a other view, similar to [color="#00BFFF"]"view.php"[/color], then, i copied the [color="#00BFFF"]"view.php"[/color] content to [color="#00BFFF"]"addTag.php"[/color] and copied the [color="#9ACD32"]"actionView"[/color] to [color="#9ACD32"]"actionAddTag"[/color] of [color="#00BFFF"]"ProjectController.php"[/color], and i added to the [color="#00BFFF"]"view.php"[/color] menu a item "addTag"


array('label'=>'Add Tags', 'url'=>array('addTag', 'id'=>$model->id))

[color="#9ACD32"]"actionAddTag"[/color]




public function actionAddTag($id)

	{

                $project=$this->loadModel($id);

		$tag=$this->actionTag_form($project);

                $this->render('addTags',array(

			'model'=>$project,

        		'tag'=>$tag,

                ));

	}



[color="#00BFFF"]"addTag.php"[/color]




<?php

$this->breadcrumbs=array(

	'Projects'=>array('index'),

	$model->title.' Add Tags',

);


$this->menu=array(

	array('label'=>'List Projects', 'url'=>array('index')),

	array('label'=>'Create Projects', 'url'=>array('create')),

	array('label'=>'Update Projects', 'url'=>array('update', 'id'=>$model->id)),

	array('label'=>'Delete Projects', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),

	array('label'=>'Manage Projects', 'url'=>array('admin')),

);


?>


<h1>View Projects (<?php echo $model->title; ?>)</h1>

<?php

    $this->widget('bootstrap.widgets.TbDetailView', array(

        'data'=>$model,

        'type' => 'striped',

        'attributes'=>array(

            array('name'=>'id'),

            array('name'=>'title'),

            array('name'=>'description'),

            array('name'=>'date'),

            array('name'=>'project_user'),

        ),

    ));

?>

<?php echo $this->renderPartial('_tags', array('model'=>$model)); ?>

<?php if(Yii::app()->user->hasFlash('success')||Yii::app()->user->hasFlash('error')):?>

    <?php $this->widget('bootstrap.widgets.TbAlert', array(

        'block'=>true, // display a larger alert block?

        'fade'=>true, // use transitions?

        'closeText'=>'&times;', // close link text - if set to false, no close link is displayed

        'alerts'=>array( // configurations per alert type

            'success'=>array('block'=>true, 'fade'=>true, 'closeText'=>'&times;'), // success, info, warning, error or danger

            'error'=>array('block'=>true, 'fade'=>true, 'closeText'=>'&times;'), // success, info, warning, error or danger

        ),));

    ?>

<?php endif; ?>

<?php

    $tags=new Tags;

    echo $this->renderPartial('_tags_form', array('model'=>$tags));

?>

<div style="clear: both;"> </div>




The problem is that this new “Add Tag” menu item in [color="#00BFFF"]“view.php”[/color] don’t link to the [color="#00BFFF"]“viewAddTag.php”[/color] view, it show an error [color="#FF0000"]403[/color]. At this point, i tested rendering the [color="#00BFFF"]“addTag.php”[/color] view in the [color="#9ACD32"]“actionView”[/color] and this works! This show the new [color="#00BFFF"]“addTag.php”[/color]

[color="#9ACD32"]"actionView"[/color]




public function actionView($id)

	{

                $project=$this->loadModel($id);

                $tag=$this->actionTag_form($project);

                $this->render('addTag',array( // This show the 'addTag.php' when clic to view a project

			'model'=>$project,

                        'tag'=>$tag,

                ));

	}



Where is the problem? is the item menu?


array('label'=>'Add Tags', 'url'=>array('addTag', 'id'=>$model->id))

Or the [color="#00BFFF"]"addTag.php"[/color]? Or [color="#9ACD32"]"actionAddTag"[/color]?

How can i solve it?

Thanks Yii!!

Hi CSNnet

What is the url of the broken generated link ?