忽略 Rails 默认布局文件



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

我有一个"

家庭"控制器,它有一个"索引"方法:

class HomeController < ActionController::Base
   def index
   end
end

以及相关的主页.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>

最后是位于布局中的应用程序.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

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

问题是你从ActionController::Base固有。你需要从 ApplicationController 进行子类化,要求 Rails 使用"application"布局作为默认值。

class HomeController < ApplicationController
   def index
   end
end

最新更新