从模型中路由辅助方法

时间:2011-03-05 07:36:18

标签: ruby-on-rails activerecord routing

评论belongs_to文章,属于问题等。

我需要返回Item的路径,该路径有相关的评论。

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

当从Comment模型执行article_path时,我会收到以下错误:

  

的未定义方法`article_path'

如何从模型中使用路由助手方法?

感谢。

1 个答案:

答案 0 :(得分:0)

你为什么要这样做?

在您的控制器/视图中使用多态路由:

<%= link_to "View comment", [@commentable, comment] %>
相关问题