title: Rails 3.1: Polymorphic Association @commentable - how to do it right?
body: <p>I am having troubles with a polymorphic association in Rails. I have an application where it should be possible to comment on different models, such as <code>Posts, Images, Projects</code></p>
  
  <p>Right now I just have Posts to comment on. On the start page there is an index view of the latest Posts and each Post has a small Comment form underneath to comment on via Ajax, very much like Facebook.</p>
  
  <p>My models look like this:</p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
    belongs_to :post_category
    belongs_to :user
  
    has_many :comments, :as =&gt; :commentable  
  
    validates_presence_of :user_id
    validates_presence_of :post_category_id
    validates_presence_of :title
    validates_presence_of :body
  
  end
  
  class Comment &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :commentable, :polymorphic =&gt; true
  end
  </code></pre>
  
  <p>Now in my Comments controller I added the following method (I think I took it from railscasts or something), which I assume tries to find out the @commentable dynamically when creating an comment.
  But this always returns the error <code>undefined method</code>comments' for nil:NilClass`</p>
  
  <pre><code>   # find commentable (parent) item
     def find_commentable
        params.each do |name, value|
          if name =~ /(.+)_id$/
             return $1.classify.constantize.find(value) unless name == 'user_id'
          end
        end
        nil
     end
  
    def create
      @commentable = find_commentable 
      @comment = @commentable.comments.build(params[:comment])
      if @comment.save
        redirect_to @comment, :notice =&gt; 'Comment was successfully created.'
        redirect_to :id =&gt; nil 
      else
        render :action =&gt; "new"
      end
    end
  </code></pre>
  
  <p>The two things I tried in my partial were:</p>
  
  <ol>
  <li><p>leaving the commentable info out of the form</p>
  
  <p>= form_for [@commentable, Comment.new], :remote => true do |f|
          #new_comment.add_comment
              = f.hidden_field :user_id, :value => current_user.id
              = f.text_field :content, :size => 55,  :value => 'leave a comment...', :class => 'comment_form'
              = f.submit "send" </p></li>
  </ol>
  
  <p>and 2. passing the commentable_id and commentable_type</p>
  
  <pre><code>= form_for [@commentable, Comment.new], :remote =&gt; true do |f|
      #new_comment.add_comment
          = f.hidden_field :user_id, :value =&gt; current_user.id
          = f.hidden_field :commentable_id, :value =&gt; post_id
          = f.hidden_field :commentable_type, :value =&gt; 'Post'
          = f.text_field :content, :size =&gt; 55,  :value =&gt; 'leave a comment...', :onfocus =&gt; 'this.select()', :class =&gt; 'comment_form'
          = f.submit "send" 
  </code></pre>
  
  <p>both without luck. Any help would be highly appreciated.
  the whole comments controller code is in this gist: <a href="https://gist.github.com/1334286" rel="nofollow">https://gist.github.com/1334286</a></p>