polymorphic_path不生成正确的路径

我正在尝试使用名为polymorphic_path的rails方法,但我得到了错误的url。 我的多态关联是学生和房东,他们都是可用的用户。

这是我的模特:

class User < ActiveRecord::Base belongs_to :userable, polymorphic: true end class Student  :userable end class Landlord  :userable end 

我有一个名为current_user的变量,它包含User对象。 以下行:

  

给我url“users / 22”而不是返回学生/房东url。

这是我的routes.rb文件,如果这有帮助..

 resources :users resources :students resources :landlords 

我哪里错了? 谢谢!

嗯,不确定polymorphic_path应该如何工作你使用它,家庭酿造替代品

 # application_controller.rb helper_method :current_profile, :path_to_profile def current_profile @current_profile ||= current_user.userable end def path_to_profile send("#{current_profile.class.downcase}_path", current_profile) end 

几乎没有额外的行你可以扩展它以使用其他方法,不仅显示。

好,我知道了! 解决方案非常明显……

 <%= link_to "Profile", polymorphic_path(current_user.userable) %> <%= link_to "Edit", edit_polymorphic_path(current_user.userable) %>