Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I guess that the <code>[@post, @post.comments.build]</code> array is just passed to <code>polymorphic_path</code> inside <code>form_for</code>. This generates a sub-resource path for comments (like <code>/posts/1/comments</code> in this case). So it looks like your first example uses comments as sub-resources to posts, right?. </p> <p>So actually the controller that will be called here is the <code>CommentsController</code>. The reason why Lukas' solution doesn't work for you might be that you actually don't use @post.comments.build inside the controller when creating the comment (it doesn't matter that you use it in the view when calling <code>form_for</code>). The <code>CommentsController#create</code> method should look like this (more or less):</p> <pre><code>def create @post = Post.find(params[:post_id] @comment = @post.comments.build(params[:comment]) if(@comment.save) # you would probably redirect to @post else # you would probably render post#show or wherever you have the form end end </code></pre> <p>Then you can use the code generated by scaffolding, only replace <code>@post</code> instance variable with <code>@comment</code> in all the lines except <code>form_for</code> call.</p> <p>I think it may also be a good idea to add the <code>@comment = @post.comment.build</code> to the controller method that displays this form and use <code>form_for([@post, @comment], ...)</code> to keep the form contents displayed in the form if there're errors.</p> <p>If this doesn't work and you're not able to figure it out, please add your <code>CommentsController#create</code> method to the question.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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