Rails默认布局文件被忽略

时间:2014-12-24 17:50:08

标签: css ruby-on-rails

我的印象是,如果您在 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

是什么给出的?我不想在每个控制器中命名我正在使用的布局。这就是应用程序级别的重点。

1 个答案:

答案 0 :(得分:4)

问题是你是从ActionController :: Base继承的。您需要从ApplicationController继承,以要求Rails使用&#34; application&#34;布局为默认值。

class HomeController < ApplicationController
   def index
   end
end
相关问题