Ruby on rails ror中的多个布局

Ruby on rails ror中的多个布局,ruby-on-rails,layout,Ruby On Rails,Layout,昨天刚开始使用Ruby on Rails。在我的布局/application.html.erb中,我有: <!DOCTYPE html> <html> <head> <title><%= full_title(yield(:title)) %></title> <%= stylesheet_link_tag "application", media: "all" %> <%

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

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= stylesheet_link_tag    "application", media: "all" %>
    <%= javascript_include_tag "application" %>
    <%= csrf_meta_tags %>  
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <%= yield %>
    </div>
    <%= render 'layouts/footer' %>
  </body>
</html> 

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

  • 布局/应用程序默认值
  • 布局/应用程序全宽(适用于全宽页面)
  • 等等
  • 在codeigniter中,您只需声明希望使用哪些模板/布局文件,但由于RubyonRails有点神奇(它为您做了很多事情),我假设它默认调用应用程序布局。我想知道是否有办法选择我想要的布局文件?

    您正在寻找该方法


    我会特别帮助你的。我想在这里提供更多细节,但前面提到的文档和指南提供了足够多的示例和使用说明。

    @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
    
    class PagesController

    application.html.erb是rails的标准布局文件。我假设它存在,并且它是默认的回退

    所有未定义的内容都将回退到application.html.erb?
    application.html.erb
    是rails的标准布局文件。我假设它存在,并且它是默认的回退,是的。