title: Creating forms for polymorphic associations in Rails
body: <p>I have a couple classes that can each have comments:</p>
  
  <pre><code>class Movie &lt; ActiveRecord::Base
      has_many :comments, :as =&gt; :commentable
  end
  
  class Actor &lt; ActiveRecord::Base
      has_many :comments, :as =&gt; :commentable
  end
  
  class Comment &lt; ActiveRecord::Base
      belongs_to :commentable, :polymorphic =&gt; true
  end
  </code></pre>
  
  <p>How do I create a form for a new movie-comment? I added </p>
  
  <pre><code>resources :movies do
      resources :comments
  end
  </code></pre>
  
  <p>to my routes.rb, and tried new_movie_comment_path(@movie), but this gives me a form containing commentable_id and commentable_type [which I want to be populated automatically, not entered by the user directly]. I also tried creating the form myself:</p>
  
  <pre><code>form_for [@movie, Comment.new] do |f|
      f.text_field :text
      f.submit
  end
  </code></pre>
  
  <p>(where "text" is a field in the Comment table)
  but this doesn't work either.</p>
  
  <p>I'm not actually sure how to associate a comment with a movie at all. For example, </p>
  
  <pre><code>c = Comment.create(:text =&gt; "This is a comment.", :commentable_id =&gt; 1, :commentable_type =&gt; "movie") 
  </code></pre>
  
  <p>doesn't seem to create a comment associated to the movie with id 1. (Movie.find(1).comments returns an empty array.)</p>