我希望在每个渲染的视图中出现一些视图,但是没有重复代码或违反规范,我似乎无法找到实现此目的的方法。
这是我当前的代码,在每个视图中调用:
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
答案 0 :(得分:5)
这可以使用rails布局和在布局中渲染部分来实现。减少对这种“建筑”的需求并使管理更容易。
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
答案 1 :(得分:2)