2014-04-04 33 views
2

我想定位app/views目錄中的一個部分,以便由兩個名稱空間共享。如何使用島嶼/ show.html.haml示例中顯示的Rails約定將缺失的部分(lostees或其他)/trees/tree.html.haml鏈接到trees/tree.html.haml?Rails常規渲染部分命名空間中的對象

配置/ routes.rb中

namespace :lostees do 
    resources :islands 
end 

namespace :others do 
    resources :islands 
end 

應用程序/控制器/ lostees/islands_controller.rb

class Lostees::IslandsController < Lostees::BaseController 
end 

應用程序/控制器/其他的/ islands_controller.rb

class Others::IslandsController < Others::BaseController 
end 

應用程序/ views/trees/_tree.html.haml

= content_tag_for tree do 
    = tree.name 

應用程序/視圖/ lostees /島嶼/ show.html.haml

%h2 The Island 

%div Trees 
= render island.trees 

#=> Missing partial lostees/trees/tree 

應用程序/視圖/人/島/ show.html.haml

%h2 The Island 

%div Trees 
= render island.trees 

#=> Missing partial others/trees/tree 

回答

1

你可能想通了這一點已,但你應該可以這樣做:

= render partial: "trees/tree", collection: island.trees 
+0

這似乎是唯一的方法來做到這一點。只是想知道是否有一種方法來堅持鐵路公約/捷徑。 – efoo

+0

我能想到的唯一其他事情就是查看Rails正在查找的部分負載路徑。我假設你沒有TreesController,否則我會期待它尋找trees/_tree.html.haml。但是,如果兩個基本控制器都繼承自ApplicationController,那麼您可能會將該部分放在application/_tree.html.haml中,並查看Rails是否在該位置找到它。我不知道這是否會工作,但它可能會。 – dvanderb