title: Easy Ruby on Rails question -- how to attach comments to both users and articles?
body: <p>I realize that this is probably a very basic question, but I have spent days coming back to it now and for some reason Google just isn't helping me. (I think part of the problem is that I'm such a beginner I'm not sure what to ask...) I've also looked in O'Reilly's Ruby Cookbook and the Rails API but I am still stuck on this issue. I found some information about polymorphic relationships but it didn't seem like that was what I needed (although let me know if I was wrong).</p>
  
  <p>I am trying to tweak <a href="http://ruby.railstutorial.org" rel="nofollow">Michael Hartl's tutorial</a> to create a blog application with users, articles, and comments (not using scaffolding). I want the comments to belong to both a user and an article.</p>
  
  <p>My main problem is: <strong>I can't figure out how to get the id of the current article into the comments controller.</strong></p>
  
  <p>The relationships for the User class:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
  
  has_many :articles
  has_many :comments, :dependent =&gt; :destroy
  </code></pre>
  
  <p>The relationships for the Article class:</p>
  
  <pre><code>class Article &lt; ActiveRecord::Base
  
  belongs_to :user
  has_many :comments, :dependent =&gt; :destroy
  </code></pre>
  
  <p>The relationships for the Comment class:</p>
  
  <pre><code>class Comment &lt; ActiveRecord::Base
  
  belongs_to :user
  belongs_to :article
  </code></pre>
  
  <p>This is my CommentsController (the about page renders in the else just to make it obvious to me for the time being):</p>
  
  <pre><code>class CommentsController &lt; ApplicationController
  before_filter :authenticate, :only =&gt; [:create, :destroy]
  
  def new
    @comment = Comment.new
  end
  
  def create
    @article = Article.find(params[:id])
    @comment = current_user.comments.build(params[:comment])
    @comment.article_id = @article.id
    if @comment.save
      flash[:success] = "Comment created!"
      redirect_to '/contact'
    else
      render '/about'
    end
  end
  
  def destroy
  end
  end
  </code></pre>
  
  <p>When I log in as a user and try to create a comment on an article, I get "Couldn't find Article without an ID." I can't figure out how to get the id of the current article into the comments controller.</p>
  
  <p>Thanks and let me know if you need me to post more code.</p>
  
  <p>Edit: Here is my _comment_form.html.erb partial which I call at the bottom of my show.html.erb view for the article:</p>
  
  <pre><code>&lt;%= form_for ([@article, @article.comments.build]) do |f| %&gt;
    &lt;div class="field"&gt;
      &lt;%= f.text_area :content %&gt;
    &lt;/div&gt;
    &lt;div class="actions"&gt;
      &lt;%= f.submit "Submit" %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Also here is the show.html.erb for the article:</p>
  
  <pre><code>&lt;heading&gt;
    &lt;h1&gt;&lt;%= @article.heading %&gt;&lt;/h1&gt;
    &lt;p&gt;Posted &lt;%= time_ago_in_words(@article.created_at) %&gt; ago by &lt;%= @article.user.name %&gt;&lt;/p&gt;
  &lt;/heading&gt;
  &lt;p&gt;&lt;%= @article.content %&gt;&lt;/p&gt;
  &lt;footer&gt;&lt;p&gt;
    &lt;% unless @article.comments.empty? %&gt;
      &lt;%= @article.comments.count %&gt;
    &lt;% end %&gt; comments&lt;/p&gt;&lt;/footer&gt;
  &lt;% unless @article.comments.empty? %&gt;
    &lt;%= render @comments %&gt;
    &lt;%= will_paginate @comments %&gt;
  &lt;% end %&gt;
  &lt;%= render 'shared/comment_form' %&gt;
  </code></pre>