Note that there are some explanatory texts on larger screens.

plurals
  1. POform_for with nested resources
    primarykey
    data
    text
    <p>I have a two-part question about form_for and nested resources. Let's say I'm writing a blog engine and I want to relate a comment to an article. I've defined a nested resource as follows:</p> <pre><code>map.resources :articles do |articles| articles.resources :comments end </code></pre> <p>The comment form is in the show.html.erb view for articles, underneath the article itself, for instance like this:</p> <pre><code>&lt;%= render :partial =&gt; "articles/article" %&gt; &lt;% form_for([ :article, @comment]) do |f| %&gt; &lt;%= f.text_area :text %&gt; &lt;%= submit_tag "Submit" %&gt; &lt;% end %&gt; </code></pre> <p>This gives an error, "Called id for nil, which would mistakenly etc." I've also tried</p> <pre><code>&lt;% form_for @article, @comment do |f| %&gt; </code></pre> <p>Which renders correctly but relates f.text_area to the article's 'text' field instead of the comment's, and presents the html for the article.text attribute in that text area. So I seem to have this wrong as well. What I want is a form whose 'submit' will call the create action on CommentsController, with an article_id in the params, for instance a post request to /articles/1/comments. </p> <p>The second part to my question is, what's the best way to create the comment instance to begin with? I'm creating a @comment in the show action of the ArticlesController, so a comment object will be in scope for the form_for helper. Then in the create action of the CommentsController, I create new @comment using the params passed in from the form_for. </p> <p>Thanks!</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.
 

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