在我的 Controller 中,我使用 layout 'application', :only => :edit
但我注意到 layout 'application', :for => :edit
也能工作。
只是好奇,因为文档只提到 :only
和 :except
。我无法在 :for
方法的任何文档中明确找到 layout
。
Rails v2.3 - layout (railsapi.com)
Rails v3.1 - layout (apidock.com)
最佳答案
正如我在上面评论的那样,Rails 忽略了您的 :for 参数,应该只对所有操作使用“应用程序”布局。
至于渲染多个布局,似乎你的 Controller 只能有一个 layout ...
行;如果您有多个,它将只使用最后一个。如果最后一个有 :only
或 :except
参数,它应该回退到所有其他操作的 application
布局。如果没有正确使用 application
布局和渲染而没有任何布局,请确保之前的开发人员没有在您的 Controller /应用程序中的某处放置类似 layout nil
的东西。
如果你想在你的 Controller 中处理多个布局,你也可以试试这个:
class UsersController < ApplicationController
layout :choose_layout
def choose_layout
if action_name == "edit"
"application"
else
"login"
end
end
end
关于ruby-on-rails - Rails 条件布局 : Why does ":for" work as an option for the layout method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8717386/