Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to submit form for one model in another's controller?
    primarykey
    data
    text
    <p>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:</p> <pre><code>class PostsController extends AppController { var $name = 'Posts'; var $uses = array('Post', 'Cmt'); function view($id = null) { ... if (!empty($this-&gt;data)) { $this-&gt;Cmt-&gt;create(); if ($this-&gt;Cmt-&gt;save($this-&gt;data)) { $this-&gt;Session-&gt;setFlash(__('The cmt has been saved', true)); } } $this-&gt;set('post', $this-&gt;Post-&gt;read(null, $id)); } </code></pre> <p>and in the view</p> <pre><code>&lt;?php echo $this-&gt;Form-&gt;create('Cmt');?&gt; &lt;fieldset&gt; &lt;?php echo $this-&gt;Form-&gt;input('name'); echo $this-&gt;Form-&gt;input('email'); echo $this-&gt;Form-&gt;input('title'); echo $this-&gt;Form-&gt;input('content'); ?&gt; &lt;div class="input select required"&gt;&lt;label for="CmtStpageId"&gt;Post&lt;/label&gt; &lt;select id="CmtPostId" name="data[Cmt][post_id]"&gt; &lt;option value="1"&gt;postname&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;?php echo $this-&gt;Form-&gt;end(__('Submit', true));?&gt; </code></pre> <p>What's wrong here that won't allow a record to be posted to the cmts table?</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload