ror中的多个布局

刚刚开始使用Ruby on Rails。 在我的layouts / application.html.erb中,我有:

          

来自php – > codeigniter背景,我假设渲染类似于$ this-> load-> view(”); 在codeigniter。
虽然这很好,但我希望有多个应用程序布局文件,例如

  1. 布局/应用程序默认
  2. 布局/应用程序全宽(对于全宽页面)
  3. 等等..

在codeigniter中你只需要声明你想要使用哪些模板/布局文件,但是作为ruby on rails有点神奇(它为你做了很多事情),我假设它默认调用应用程序布局。 我想知道是否有办法选择我想要的布局文件?

你正在寻找layout方法。

此Rails指南将帮助您,特别是查找布局 。 我在这里提供了更多细节,但前面提到的文档和指南提供了足够的示例和使用说明。

@Deefour提供了正确的资源,这是一个很好的快速示例,说明如何在Rails 4中实现这一点。

在控制器中,您可以指定要为特定操作获取布局的位置,并对使用哪种布局进行非常精细的控制。

 class PagesController < ApplicationController layout "fullwidth", except: [:index, :faqs] def popout # Render this action with specific layout render layout: "popout" #renders with views/layouts/popout.html.erb end def index #renders with views/layouts/application.html.erb end def about_us #renders with views/layouts/fullwidth.html.erb end def team #renders with views/layouts/fullwidth.html.erb end def logo #renders with views/layouts/fullwidth.html.erb end def faqs #renders with views/layouts/application.html.erb end end 

application.html.erb是rails标准布局文件。 我认为它存在,它是默认的后备!