首先,我们将该控制器称为“遗留”控制器.它已经存在了很长时间,并且做了很多事情错了,但是我现在不想重构它,所以我试图找到办法来处理我们的事情.
新的动作是这样的(在BarsController中)
def new if something render :partial => "foo",:layout => "bars" elsif something_else render :partial => "foo2",:layout => "bars" elsif something_else_else render :partial => "foo3",:layout => "bars" else render :partial => "foo4",:layout => "bars" end
现在,在Rails 2.3.5中,这很好.它会使相应的部分在适当的布局中 – 我意识到布局选项在这里是冗余的,因为它将默认为条形布局.当我们升级到Rails 3.0.x时,我们开始收到以下错误:
Missing partial layouts/bars with {:handlers=>[:erb,:rjs,:builder,:rhtml,:rxml],:formats=>[:html]
显然,layout / bars.html.erb文件是一直存在的,所以我无法弄清楚.我能够渲染:layout =>假的,但那当然不行.最终我知道如果我做以下任何一个,它的作品:
1)将我的布局重命名为_bars.html.erb而不是bars.html.erb和:
render:partial => ‘foo2’,:layout => “酒吧”
2)保持我的布局为bars.html.erb(我想要的)和:
这里渲染’_foo2’#:部分选项是冗余的
似乎通过使用:partial选项而不是字符串,因为第一个参数是导致rails将_name.html.erb约定应用于部分和布局.如果我自己放入下划线,那就回到我预期的行为,而不是预设布局名称的前面.
有人知道为什么会这样吗?
编辑好的,不知道我怎么错过了这个…但是这里的文档提到了这一点.看起来好像是从2.3.8开始,可能在2.3.5(我们正在运行的)中做的不一样?
3.4.3 Partial Layouts
A partial can use its own layout file,just as a view can use a
layout. For example,you might call a partial like this:<%= render “link_area”,:layout => “graybar” %> This would look for a
partial named _link_area.html.erb and render it using the layout
_graybar.html.erb. Note that layouts for partials follow the same
leading-underscore naming as regular partials,and are placed in the
same folder with the partial that they belong to (not in the master
layouts folder).
解决方法
由于Rails 2.3.8,在渲染部分渲染时会出现默认行为:partial => ‘foo’,:layout => ‘bars’是期望一个“局部布局”文件以及部分视图文件.在这种情况下,它会期待
app / views / _foo.html.erb以及app / views / layouts / _bars.html.erb
对于从Rails 2.3.5升级的任何人遇到这个问题,我发现这个解决方案的影响最小:
render’_foo’,:layout => “酒吧”
此解决方案不会假定您呈现部分,因此不会期望部分布局.另一个选项是复制你的布局
应用程序/视图/布局/ _bars.html.erb
并使用
render:partial => ‘foo’,:layouts => “酒吧”
但是这会导致代码重复.
铁路2.3.8有关这方面的文件:
3.4.3 Partial Layouts
A partial can use its own layout file,:layout => “graybar” %> This would look for a partial named _link_area.html.erb and render it using the layout _graybar.html.erb. Note that layouts for partials follow the same leading-underscore naming as regular partials,and are placed in the same folder with the partial that they belong to (not in the master layouts folder).