0

Comment belongs_to Article, belongs to Question etc.

I need to return the path to Item, that has a comment in question.

class Comment < ActiveRecord::Base
   belongs_to :commentable, :polymorphic => true

   def commentable_path
     return case self.commentable_type
       when "Article": article_path(self.commentable)
       when "Question": question_path(self.commentable)
     end
   end
end

When article_path will be executed from Comment model, i get following error:

undefined method `article_path' for #

How to utilize routing helper methods from a model ?

Thanks.

AntonAL
  • 16,692
  • 21
  • 80
  • 114
  • possible duplicate of [Can Rails Routing Helpers (i.e. mymodel_path(model)) be Used in Models?](http://stackoverflow.com/questions/341143/can-rails-routing-helpers-i-e-mymodel-pathmodel-be-used-in-models) – AntonAL Mar 05 '11 at 08:21

1 Answers1

0

Why would you do this?

In your controllers / views use polymorphic routing instead:

<%= link_to "View comment", [@commentable, comment] %>
Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261