Ruby on rails 正在查找具有多种父类型的嵌套资源的父级

Ruby on rails 正在查找具有多种父类型的嵌套资源的父级,ruby-on-rails,Ruby On Rails,假设模型如下: class PhoneNumber < ActiveRecord::Base has_many :personal_phone_numbers has_many :household_phone_numbers has_many :organization_phones has_many :people, :through => :personal_phone_numbers has_many :households, :through =>

假设模型如下:

class PhoneNumber < ActiveRecord::Base
  has_many :personal_phone_numbers
  has_many :household_phone_numbers
  has_many :organization_phones

  has_many :people, :through => :personal_phone_numbers
  has_many :households, :through => :household_phone_numbers
  has_many :organizations, :through => :organization_phones
end
classphonenumber:个人电话号码
有很多:家庭,:通过=>:家庭电话号码
有许多:组织,:通过=>:组织\u电话
结束
查看电话号码时,我可能会将其视为嵌套资源,因此控制器将具有
个人id
家庭id
组织id


我需要该视图有一个指向“Return”的
链接,…
返回到我们找到电话号码的正确资源。我应该怎么做?

如果您使用的是嵌套资源,那么您需要在PhoneNumberController上设置父对象的before筛选器,这样您就可以执行
@parent.phone\u numbers.build(…)
对吗?同时(在筛选之前),设置
@parent\u path
organization\u path
househouse\u path
…),您将在视图中可以链接到该路径


如果您使用
:polymorphic=>true
而不是
has\u many:to
,将电话号码添加到这些内容中的每一项(组织、家庭…),您只需在筛选之前在PhoneNumbersController
中设置@parent,然后执行
多态路径(@parent)
取而代之。

如果在控制器中设置了@parent,并且您有restful路由,您可以只做
链接到“Return”、@parent
实际上,我不知道magic Polymopic url做了什么。在我看来,我所要做的就是把@parent链接到“Return”上,它神奇地知道该返回什么资源。