ruby-on-rails - rr 中的多个布局

标签 ruby-on-rails layout

昨天刚开始 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> 

来自 php ->codeigniter 背景,我假设 render 类似于 $this->load->view('');在代码点火器中。
虽然这很好用,但我想要多个应用程序布局文件,例如
  • 布局/应用程序默认
  • 布局/应用程序全角(用于全角页面)
  • 等等..

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

    最佳答案

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

    在 Controller 中,您可以指定要在何处获取特定操作的布局,并对使用的布局进行非常精细的控制。

    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 标准布局文件。我假设它存在,并且它是默认的后备!

    关于ruby-on-rails - rr 中的多个布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15228228/

    相关文章:

    ruby-on-rails - ruby 内置方法的定义

    javascript - 引用错误: $ is not defined function error

    ruby-on-rails - 将值数组发送到 ruby​​ 中的 sql 查询?

    android - RelativeLayout如何给布局权重?

    html - 100%高度容器通过绝对定位被推出IE视口(viewport)

    mysql - 在 ruby​​ 项目中将 mysql 更改为 postgresql 的最佳方法是什么

    ruby-on-rails - 如何从 url 获取子域值?

    css - 拉伸(stretch)水平 ul 以适应 div 的宽度

    html - 由于内容过多,页脚错位

    java - 为什么我不能移动(定位)按钮