2015-01-03 124 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