Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you're going about this in the wrong way. </p> <p><strong>First: I really hope that you're not calling a model <code>Thread</code>, as that would conflict with the Ruby class <code>Thread</code>. If you are, choose a different word</strong>.</p> <p>Now with the "please aim the gun <em>away</em> from your foot" message out of the way...</p> <hr> <p>You shouldn't be calling out to the <code>MessagesController</code> to create a new message for the controller. Instead, you should be using nested attributes in the new thread form:</p> <pre><code>&lt;%= form_for @thread do |f| %&gt; &lt;%= f.fields_for :messages do |message| %&gt; &lt;%= render "messages/form", :message =&gt; message %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>Inside your <code>DiscussionThread</code> (I am assuming the name of it here) model, you would then have these lines:</p> <pre><code>has_many :messages accepts_nested_attributes_for :messages </code></pre> <p>You may have to add <code>messages_attributes</code> to the <code>attr_accessible</code> attributes in this model too.</p> <p>This tells the <code>DiscussionThread</code> model that instances can accept attributes for the <code>messages</code> association too. In your <code>ThreadsController</code> the action would then remain the same.</p> <p>For more information about nested attributes, I recommend watching the Railscasts on <a href="http://railscasts.com/episodes/196-nested-model-form-part-1" rel="nofollow">Nested Forms #1</a> and <a href="http://railscasts.com/episodes/197-nested-model-form-part-2" rel="nofollow">Nested Forms #2</a>.</p>
 

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