【问题标题】:Rails default layout file ignoredRails 默认布局文件被忽略
【发布时间】:2014-12-24 17:50:08
【问题描述】:

我的印象是,如果您的 layouts 目录中有名为 application.html.erb 的文件,则此布局将自动应用于所有视图,而无需显式引用它。 情况似乎并非如此。

我有一个“家庭”控制器,它有一个“索引”方法:

class HomeController < ActionController::Base
   def index
   end
end

以及相关的home.html.erb查看页面:

<h2>Welcome!</h2>
<div>Stay tuned for basic functions to start arriving on this site.</div>
<div>The site will not look very stylish until one of the bounties gets done about writing the Style guide.</div>

最后是位于布局中的 application.html.erb 文件:

<!DOCTYPE html>
<html>
<head>
    <title>App Title</title>
    <%= stylesheet_link_tag "application", media: "all" %>
</head>
<body>
  <div class="navbar">
    <div class="navbar-inner">
        <a href="/categories/index">Categories</a>
    </div>
  </div>

  <%= yield %>

<div class="footer">Michael</div>
</body>
</html>

在我像这样在我的家庭控制器中添加对布局的显式引用之前,上面的文件被忽略了:

class HomeController < ActionController::Base
   layout 'application'
   def index
   end
end

什么给了?我不想命名我在每个控制器中使用的布局。这就是在应用程序级别拥有它的意义所在。

【问题讨论】:

    标签: css ruby-on-rails


    【解决方案1】:

    问题是您继承自 ActionController::Base。您需要从 ApplicationController 子类化以要求 Rails 使用“应用程序”布局作为默认布局。

    class HomeController < ApplicationController
       def index
       end
    end
    

    【讨论】:

      猜你喜欢
      • 2018-12-29
      • 2022-01-13
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多