ror中的多个布局

Sky*_*ist 7 layout ruby-on-rails

刚刚开始使用Ruby on Rails.在我的layouts/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> 
Run Code Online (Sandbox Code Playgroud)

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

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

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

mah*_*ich 14

@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
Run Code Online (Sandbox Code Playgroud)

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


dee*_*our 8

你正在寻找这种layout方法.

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