2016-09-13 145 views
1

我試圖在這個例子中創建Yii 1.1中的評論http://yiiframework.ru/doc/blog/en/comment.create,但是我在帖子頁面(應該在頁面底部呈現內容和評論表單)上有404錯誤。我想我已經在PostController中的actionView錯誤如示例代碼Yii評論系統

$post=$this->loadModel(); 
    $comment=$this->newComment($post); 

    $this->render('view',array(
     'model'=>$post, 
     'comment'=>$comment, 
    )); 

,並在我的控制器INTIAL代碼爲$this->render('view',array('model'=>$this->loadModel($id),

我創建註釋模型在GII開創了污物評論。

public function actionView($id) 
    { 
      $post=$this->loadModel(); 
      $comment=$this->newComment($post); 

      $this->render('view',array(
      'model'=>$post, 
      'comment'=>$comment, 
      //$this->loadModel($id),     

     )); 
    } 

protected function newComment($post) 
{ 
    $comment=new Comment; 
    if(isset($_POST['Comment'])) 
    { 
     $comment->attributes=$_POST['Comment']; 
     if($post->addComment($comment)) 
     { 
      if($comment->status==Comment::STATUS_PENDING) 
       Yii::app()->user->setFlash('commentSubmitted','Thank you for your comment. Your comment will be posted once it is approved.'); 
      $this->refresh(); 
     } 
    } 
    return $comment; 
} 

在Post模型:

public function addComment($comment) 
    { 
     if(Yii::app()->params['commentNeedApproval']) 
      $comment->status=Comment::STATUS_PENDING; 
     else 
      $comment->status=Comment::STATUS_APPROVED; 
     $comment->post_id=$this->id; 
     return $comment->save(); 
    } 

在保護/視圖/後/ view.php

<div id="comments"> 
    <?php if($model->commentCount>=1): ?> 
     <h3> 
      <?php echo $model->commentCount . 'comment(s)'; ?> 
     </h3> 

     <?php $this->renderPartial('_comments',array(
      'post'=>$model, 
      'comments'=>$model->comments, 
     )); ?> 
    <?php endif; ?> 
</div> 
+0

在你問題的最後一行,你的意思是yii? – ShellZero

+0

顯示調用acction的代碼/網址。然後顯示所有的行動代碼(聲明行動功能太) – scaisEdge

+0

添加了代碼。 – rick1

回答

0

在你的actionView方法,你是不是過的$ id傳遞給loadModel()。它應該是這樣的。

public function actionView($id) 
    { 
      $post=$this->loadModel($id); 
      $comment=$this->newComment($post); 

      $this->render('view',array(
      'model'=>$post, 
      'comment'=>$comment, 
      //$this->loadModel($id),     

     )); 
    } 

否則,loadModel方法將拋出一個404錯誤,因爲它找不到任何要載入的東西。