title: Polymorphic behavior with link_to
body: <p>I'm facing kind of a tricky challenge here, maybe one of you could help me on this. Nothing too hard, I just don't know the good way to go. Here comes:</p>
  
  <p>I have 2 models:</p>
  
  <p><code>Activity</code> and <code>Comment</code></p>
  
  <p>I'm using MongoID here with a famous gem to handle votes (likes). It only take care of embedding a vote model inside an <code>Activity</code> or a <code>Comment</code>.</p>
  
  <p>This means that I don't have a <code>Like</code> model.</p>
  
  <p>How ever I would like to be able to "like" an "activity" and/or a "comment". My guess is to go with a single Controller named <code>LikesController</code> with 2 methods for both liking or unliking an item.</p>
  
  <p>I would then have this kind of routes:</p>
  
  <pre><code>resources :activities,  :only =&gt; [] do
    resources :comments,  :only =&gt; [:create, :destroy]
    resources :likes,     :only =&gt; [:create, :destroy]
  end
  
  resources :comments, :only =&gt; [] do
    resources :likes,  :only =&gt; [:create, :destroy]
  end
  </code></pre>
  
  <p>Here are the given routes from <code>bundle exec rake routes</code>:</p>
  
  <pre><code>activity_comments POST   /activities/:activity_id/comments(.:format)     comments#create
  activity_comment  DELETE /activities/:activity_id/comments/:id(.:format) comments#destroy
  activity_likes    POST   /activities/:activity_id/likes(.:format)        likes#create
  activity_like     DELETE /activities/:activity_id/likes/:id(.:format)    likes#destroy
  comment_likes     POST   /comments/:comment_id/likes(.:format)           likes#create
  comment_like      DELETE /comments/:comment_id/likes/:id(.:format)       likes#destroy
  </code></pre>
  
  <p>Now I would like to have a "Like" / "Unlike" link in some view page pointing to the <code>LikesController</code> on the corresponding actions (create => like ; destroy => unlike).</p>
  
  <p>The only thing is that I don't know how to create the <code>link_to</code> with the good path.
  Here is what I have so far:</p>
  
  <p><code>link_to 'Like', eval("#{item.class.name.downcase}_likes_path(item)"), :method =&gt; 'post'</code></p>
  
  <p><code>link_to 'Unlike, eval("#{item.class.name.downcase}_likes_path(item)"), :method =&gt; 'destroy'</code></p>
  
  <p>Can anyone give me a tip on how to do that in a cleaner way ?</p>
  
  <p>Here are my models:</p>
  
  <pre><code>class Activity
    include Mongoid::Document
    include Mongoid::Timestamps
    include Mongo::Voteable
  
    has_many    :comments,  :as =&gt; :item
    belongs_to  :subject,   :polymorphic =&gt; true
    embeds_one  :user
  
    validates :user, :presence =&gt; true
  
    voteable self, :up =&gt; +1, :down =&gt; -1
  end
  
  class Comment
    include Mongoid::Document
    include Mongoid::Timestamps
    include Mongo::Voteable
  
    belongs_to :item, :polymorphic =&gt; true
    embeds_one :user
  
    field :text, :type =&gt; String, :default =&gt; nil
  
    validates :text, :presence =&gt; true
    validates :user, :presence =&gt; true
  
    attr_accessible :text, :user
  
    voteable self, :up =&gt; +1, :down =&gt; -1
  end
  </code></pre>
  
  <p>You have to keep in mind that a lot of futur objects could be "likeable" also, that's why I choose to go with a <code>LikesController</code> beside having duplicate method inside each item's controller to handle "likes" and "unlikes". Maybe I'm wrong ?</p>
  
  <p>Thank you.</p>