2015-01-03 76 views
0

我有基本的设置多态性网址:轨创建多态模型

class Document < ActiveRecord::Base 
    belongs_to :documentable, polymorphic: true 
end 

class Contact < ActiveRecord::Base 
    has_many :documents, as: :documentable 
end 

class Case < ActiveRecord::Base 
    has_many :documents, as: :documentable 
end 

现在_index.html.erb的我的文档查看,我要做到以下几点:

<%= link_to "New Document", polymorphic_path([:new, @documentable, Document.new]) %> 

哪里@documentable将成为联系人或案例的实例。

我期望上面生成一个url像new_contact_document_path,但它只是试图产生一个url像new_documents_path。

我会做什么错?

回答

0

尝试

<%= link_to "New Document", new_polymorphic_path([@documentable, Document]) %> 

注意这里有两点不同,从您发布的代码:

  1. 使用,而不是嵌入新的动作传递的数组里面
  2. 使用Document而不是“前缀” polymorphic_path帮手的Document.new,这似乎是首选方法

查看the ActionDispatch docs了解更多详情。

+0

这也适用于许多嵌套的路线:new_polymorphic_path([@ model1,@ model2,Model3]) – Donato