How do I submit the form for one template in another's controller?


Let's say I have a Posts table and a Comments table. I want my /posts/view/ page to have a form on the same page for submitting a comment, much like any typical blog. I'm not sure where I'm going wrong here, but this is what I've tried:

class PostsController extends AppController {
var $name = 'Posts';
var $uses = array('Post', 'Cmt'); 

function view($id = null) {
    if (!empty($this->data)) {
        if ($this->Cmt->save($this->data)) {
            $this->Session->setFlash(__('The cmt has been saved', true));

    $this->set('post', $this->Post->read(null, $id));

and in the view

<?php echo $this->Form->create('Cmt');?>
    echo $this->Form->input('name');
    echo $this->Form->input('email');
    echo $this->Form->input('title');
    echo $this->Form->input('content');
    <div class="input select required"><label for="CmtStpageId">Post</label>
        <select id="CmtPostId" name="data[Cmt][post_id]">
            <option value="1">postname</option>
<?php echo $this->Form->end(__('Submit', true));?>

What's wrong here that won't allow a record to be posted to the cmts table?

Also, I have the post id hardcoded into that form, as you can see, because the select box doesn't populate with post id's for whatever reason. Any help with that would be appreciated also.

When you create a form, you can explicitly set what the form action is using the url parameter:

$this->Form->create('Cmt', array('url'=>$this->Html->url(array('controller'=>'cmts', 'action'=>'add'))));

As for the Post ID, I assume that you have a 1-to-Many relationship between posts and comments. If that's the case, you should just be able to do the following in your view: echo $this->Form->input('post_id', array('type'=>'hidden')); Then, in your view function, set $this->data['Cmt']['post_id'] = $post['Post']['id']; to get it to automatically populate.