2010-03-07 244 views
3

我有幾個視圖我希望在呈現的每個視圖上都顯示,但缺少重複代碼或違反規範,我似乎無法找到一種方法來完成這個。從另一個視圖呈現視圖

這裏是我當前的代碼,將調用每個視圖:

 
def ImOnABoat::Views 
    def layout 
    html do 
     head do title "Petstore" end 
     body do yield end 
    end 
    end 

    def navigation 
    p "Welcome to our tiny petstore!" 
    end 

    def poodle 
    navigation # Have to duplicate this in every view 
    p "We have a poodle!" 
    end 

    def fluffy_bunny 
    navigation # Have to duplicate this in every view 
    p "Come see-- OH CRAP IT'S A VELOCIRAPTOR!" 
    end 
end 

我也可以把它通過允許公共塊在身體外渲染工作,但這是針對規範,將可能最終可怕打破一些刮板腳本。

 
def layout 
    def head do title "Petstore" end 
    nav # This is not inside <body>! 
    def body do yield end 
end 

回答

2

請記住,這是普通的紅寶石,所以你可以簡單地調用在佈局中navigation方法:

module ImOnABoat::Views 
    def layout 
    html do 
     head { title "Petstore" } 
     body do 
     navigation 
     yield 
     end 
    end 
    end 

    def navigation 
    p "Welcome to our tiny petstore!" 
    end 

    def poodle 
    p "We have a poodle!" 
    end 

    def fluffy_bunny 
    p "Come see-- OH CRAP IT'S A VELOCIRAPTOR!" 
    end 
end 
相關問題