Sunday, February 28, 2010

Missing Action

I'm working through a tutorial (Beginning CakePHP book) and am getting
an error I don't understand. The error says "Error: The action vote is
not defined in controller CommentsController" and "Error: Create
CommentsController::vote() in file: app/controllers/
comments_controller.php." However, this is what my app/controllers/
comments_controller.php file looks like:

<?
class CommentsController extends AppController {
var $name = 'Comments';

function add() {
if(!empty($this->data)){
$this->Comment->create();
if($this->Comment->save($this->data)) {
$comments=$this->Comment-
>find('all',array('conditions'=>array('post_id'=>$this->data['Comment']
['post_id']),'recursive'=>-1);
$this->set(compact('comments'));
$this->render('add_success','ajax');
}else{
$this->render('add_failure','ajax');
}
}
}

function vote($type=null,$id=null) {
if($id){
$votes=$this->Comment->vote($type,$id);
$this->set(compact('votes'));
$this->render('votes','ajax');
}
}
}
?>

I've confirmed that the add action works by loading /comments/add, so
it's finding that action, why can't it find the vote action?

The portion of the application that deals with this is in /app/views/
posts/view.ctp, which looks like this:

<?=$html->link('<h1>' . $post['Post']['name'] . '</h1>', '/posts/
view/' . $post['Post']['id'],
null, null, false); ?>
<p>Author: <?=$post['User']['firstname']; ?> <?=$post['User']
['lastname']; ?></p>
<p>Date Published: <?=$time->nice($post['Post']['date']); ?></p>
<hr />
<p><?=$post['Post']['content']; ?></p>
<hr />
<h2>Comments</h2>
<div id="comments">
<? foreach ( $comments as $comment ): ?>
<div class="comment">
<div id="vote_<?=$comment['Comment']['id']; ?>">
<div class="cast_vote">
<ul>
<?=$ajax->link( '<li>up</li>',
'/comments/vote/up/' . $comment['Comment']
['id'],
array( 'update' => 'vote_' .
$comment['Comment']['id'] ), null, false ); ?>
<?=$ajax->link( '<li>up</li>',
'/comments/vote/down/' . $comment['Comment']
['id'],
array( 'update' => 'vote_' .
$comment['Comment']['id'] ), null, false ); ?>
</ul>
</div>
<div class="vote"><?=$comment['Comment']['votes']; ?></div>
</div>
<p><b><?=$comment['Comment']['name']; ?></b></p>
<p><?=$comment['Comment']['content']; ?></p>
</div>
<? endforeach; ?>
<?=$ajax->form( array( 'type' => 'post',
'options' => array( 'model' => 'Comment',
'update' => 'comments',
'url' =>
array( 'controller' => 'comments',
'action'
=> 'add' ) ) ) ); ?>
<?=$form->input( 'Comment.name' ); ?>
<?=$form->input( 'Comment.content' ); ?>
<?=$form->input( 'Comment.post_id', array( 'type' => 'hidden',
'value' => $post['Post']
['id'] ) ); ?>
<?=$form->end( 'Add Comment' ); ?>
</div>

Any help would be appreciated.

Thanks,
Chuck

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: